diff options
author | Minh Nguyễn <mxn@1ec5.org> | 2016-03-17 16:37:37 -0700 |
---|---|---|
committer | Minh Nguyễn <mxn@1ec5.org> | 2016-03-18 14:02:32 -0700 |
commit | b493bdc649b4e32cbedb3fd5cb9e3e4157dfdb34 (patch) | |
tree | b46f2d6f5e2781e4ee8e5a38747039ff8ce9a5d5 /platform/ios | |
parent | fff13d0976015650c2c79348dd8d51d25ce0b2d1 (diff) | |
download | qtlocation-mapboxgl-b493bdc649b4e32cbedb3fd5cb9e3e4157dfdb34.tar.gz |
[ios, osx] Unified offline and ambient caches
There is now only one instance of mbgl::OfflineFileSource, created when the shared MGLOfflineStorage object is initialized. Also create and use the shared MGLOfflineStorage object when initializing an MGLMapView object. The unified cache file is located in a subdirectory of Application Support, where the SDK has control over the file’s lifetime. The subdirectory is already named after the host application’s bundle identifier, ensuring that each Mapbox-powered application has an independent tile limit. If there’s an ambient cache in a subdirectory of Caches, delete it. If there’s an offline cache in a subdirectory of Documents on iOS or Caches on OS X, move it to the unified cache location in a subdirectory of Application Support.
Fixes the iOS/OS X side of #4338.
Diffstat (limited to 'platform/ios')
-rw-r--r-- | platform/ios/src/MGLMapView.mm | 42 |
1 files changed, 8 insertions, 34 deletions
diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 21d15ed1dd..2def0c54f0 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -30,6 +30,7 @@ #import "Mapbox.h" #import "../../darwin/src/MGLGeometry_Private.h" #import "../../darwin/src/MGLMultiPoint_Private.h" +#import "../../darwin/src/MGLOfflineStorage_Private.h" #import "NSBundle+MGLAdditions.h" #import "NSString+MGLAdditions.h" @@ -37,7 +38,6 @@ #import "NSException+MGLAdditions.h" #import "MGLUserLocationAnnotationView.h" #import "MGLUserLocation_Private.h" -#import "MGLAccountManager_Private.h" #import "MGLAnnotationImage_Private.h" #import "MGLMapboxEvents.h" #import "MGLCompactCalloutView.h" @@ -187,7 +187,6 @@ public: { mbgl::Map *_mbglMap; MBGLView *_mbglView; - mbgl::DefaultFileSource *_mbglFileSource; BOOL _opaque; @@ -303,18 +302,15 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) // setup mbgl view const float scaleFactor = [UIScreen instancesRespondToSelector:@selector(nativeScale)] ? [[UIScreen mainScreen] nativeScale] : [[UIScreen mainScreen] scale]; _mbglView = new MBGLView(self, scaleFactor); - - // setup mbgl cache & file source - NSString *fileCachePath = @""; + + // Delete the pre-offline ambient cache at ~/Library/Caches/cache.db. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); - if ([paths count] != 0) { - NSString *libraryDirectory = [paths objectAtIndex:0]; - fileCachePath = [libraryDirectory stringByAppendingPathComponent:@"cache.db"]; - } - _mbglFileSource = new mbgl::DefaultFileSource([fileCachePath UTF8String], [[[[NSBundle mainBundle] resourceURL] path] UTF8String]); + NSString *fileCachePath = [paths.firstObject stringByAppendingPathComponent:@"cache.db"]; + [[NSFileManager defaultManager] removeItemAtPath:fileCachePath error:NULL]; // setup mbgl map - _mbglMap = new mbgl::Map(*_mbglView, *_mbglFileSource, mbgl::MapMode::Continuous, mbgl::GLContextMode::Unique, mbgl::ConstrainMode::None); + mbgl::DefaultFileSource *mbglFileSource = [MGLOfflineStorage sharedOfflineStorage].mbglFileSource; + _mbglMap = new mbgl::Map(*_mbglView, *mbglFileSource, mbgl::MapMode::Continuous, mbgl::GLContextMode::Unique, mbgl::ConstrainMode::None); // start paused if in IB if (_isTargetingInterfaceBuilder || background) { @@ -322,13 +318,6 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) _mbglMap->pause(); } - // Observe for changes to the global access token (and find out the current one). - [[MGLAccountManager sharedManager] addObserver:self - forKeyPath:@"accessToken" - options:(NSKeyValueObservingOptionInitial | - NSKeyValueObservingOptionNew) - context:NULL]; - // Notify map object when network reachability status changes. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) @@ -506,7 +495,6 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; - [[MGLAccountManager sharedManager] removeObserver:self forKeyPath:@"accessToken"]; [_attributionButton removeObserver:self forKeyPath:@"hidden"]; [self validateDisplayLink]; @@ -517,12 +505,6 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) _mbglMap = nullptr; } - if (_mbglFileSource) - { - delete _mbglFileSource; - _mbglFileSource = nullptr; - } - if (_mbglView) { delete _mbglView; @@ -1606,15 +1588,7 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(__unused void *)context { - // Synchronize mbgl::Map’s access token with the global one in MGLAccountManager. - if ([keyPath isEqualToString:@"accessToken"] && object == [MGLAccountManager sharedManager]) - { - NSString *accessToken = change[NSKeyValueChangeNewKey]; - if (![accessToken isKindOfClass:[NSNull class]]) { - _mbglFileSource->setAccessToken((std::string)[accessToken UTF8String]); - } - } - else if ([keyPath isEqualToString:@"hidden"] && object == _attributionButton) + if ([keyPath isEqualToString:@"hidden"] && object == _attributionButton) { NSNumber *hiddenNumber = change[NSKeyValueChangeNewKey]; BOOL attributionButtonWasHidden = [hiddenNumber boolValue]; |