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/darwin/src/MGLOfflineStorage.mm | |
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/darwin/src/MGLOfflineStorage.mm')
-rw-r--r-- | platform/darwin/src/MGLOfflineStorage.mm | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index 3eac28ca78..1f925b3ee2 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -22,18 +22,20 @@ static dispatch_once_t onceToken; static MGLOfflineStorage *sharedOfflineStorage; dispatch_once(&onceToken, ^{ - sharedOfflineStorage = [[self alloc] initWithFileName:@"offline.db"]; + sharedOfflineStorage = [[self alloc] initWithFileName:@"cache.db"]; }); return sharedOfflineStorage; } +// This method can’t be called -init, because that selector has been marked +// unavailable in MGLOfflineStorage.h. - (instancetype)initWithFileName:(NSString *)fileName { if (self = [super init]) { -#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR - NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); - NSString *cachePath = [paths.firstObject stringByAppendingPathComponent:fileName]; -#elif TARGET_OS_MAC - NSURL *cacheDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory + // Place the cache in a location specific to the application, so that + // packs downloaded by other applications don’t count toward this + // application’s limits. + // ~/Library/Application Support/tld.app.bundle.id/cache.db + NSURL *cacheDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES @@ -46,7 +48,34 @@ error:nil]; NSURL *cacheURL = [cacheDirectoryURL URLByAppendingPathComponent:fileName]; NSString *cachePath = cacheURL ? cacheURL.path : @""; + + // Move the offline cache from v3.2.0-beta.1 to a location that can also + // be used for ambient caching. + NSString *legacyCacheFileName = @"offline.db"; +#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR + // ~/Documents/offline.db + NSArray *legacyPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); + NSString *legacyCachePath = [legacyPaths.firstObject stringByAppendingPathComponent:legacyCacheFileName]; +#elif TARGET_OS_MAC + // ~/Library/Caches/tld.app.bundle.id/offline.db + NSURL *legacyCacheDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory + inDomain:NSUserDomainMask + appropriateForURL:nil + create:YES + error:nil]; + legacyCacheDirectoryURL = [legacyCacheDirectoryURL URLByAppendingPathComponent: + [NSBundle mainBundle].bundleIdentifier]; + [[NSFileManager defaultManager] createDirectoryAtURL:legacyCacheDirectoryURL + withIntermediateDirectories:YES + attributes:nil + error:nil]; + NSURL *legacyCacheURL = [legacyCacheDirectoryURL URLByAppendingPathComponent:legacyCacheFileName]; + NSString *legacyCachePath = legacyCacheURL ? legacyCacheURL.path : @""; #endif + if (![[NSFileManager defaultManager] fileExistsAtPath:cachePath]) { + [[NSFileManager defaultManager] moveItemAtPath:legacyCachePath toPath:cachePath error:NULL]; + } + _mbglFileSource = new mbgl::DefaultFileSource(cachePath.UTF8String, [NSBundle mainBundle].resourceURL.path.UTF8String); // Observe for changes to the global access token (and find out the current one). |