Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Make offline database path configurable #298

Merged
merged 2 commits into from
May 6, 2020
Merged

Conversation

1ec5
Copy link
Contributor

@1ec5 1ec5 commented May 6, 2020

Added the MGLOfflineStorage.databasePath and MGLOfflineStorage.databaseURL properties to obtain the path of the database that contains offline packs and the ambient cache, as well as an MGLOfflineStorageDatabasePath Info.plist key to customize this path.

Fixes #297.

/cc @mapbox/maps-ios

@1ec5 1ec5 added enhancement New feature or request ios macOS labels May 6, 2020
@1ec5 1ec5 added this to the release-water milestone May 6, 2020
@1ec5 1ec5 requested a review from a team May 6, 2020 01:09
@1ec5 1ec5 self-assigned this May 6, 2020
@1ec5 1ec5 force-pushed the 1ec5-offline-db-path-297 branch from b7e3ddd to 696a99d Compare May 6, 2020 01:22
add or remove custom parameters, or reroute certain requests to other servers
or endpoints.
*/
@property(nonatomic, weak, nullable) IBOutlet id<MGLOfflineStorageDelegate> delegate;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn’t new; I just moved it up above the database merging methods.

The subdirectory-less cache was located at:
~/Library/Application Support/tld.app.bundle.id/cache.db
*/
+ (NSURL *)cacheURLIncludingSubdirectory:(BOOL)useSubdirectory {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously these class methods were sandwiched below certain methods related to +sharedOfflineStorage and above -init. I’ve consolidated them down where the other database-related methods are implemented and renamed them for consistency, but otherwise I haven’t changed them.

Comment on lines -234 to -246
// Move the offline cache from v3.2.0-beta.1 to a location that can also
// be used for ambient caching.
if (![[NSFileManager defaultManager] fileExistsAtPath:cachePath]) {
NSString *legacyCachePath = [[self class] legacyCachePath];
[[NSFileManager defaultManager] moveItemAtPath:legacyCachePath toPath:cachePath error:NULL];
}

// Move the offline file cache from v3.2.x path to a subdirectory that
// can be reliably excluded from backups.
if (![[NSFileManager defaultManager] fileExistsAtPath:cachePath]) {
NSURL *subdirectorylessCacheURL = [[self class] cacheURLIncludingSubdirectory:NO];
[[NSFileManager defaultManager] moveItemAtPath:subdirectorylessCacheURL.path toPath:cachePath error:NULL];
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These preflight steps have been moved into a lazily initialized -offlineURL getter.

Comment on lines +232 to +235
[NSFileManager.defaultManager createDirectoryAtURL:directoryURL
withIntermediateDirectories:YES
attributes:nil
error:nil];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with the default code path in -defaultDatabaseURLIncludingSubdirectory:, this custom path code path results in the containing directory being created if it doesn’t already exist. However, this code path doesn’t exclude the containing directory from iCloud backups. Info.plist could’ve specified a path in the same folder as other resources. Besides, the developer might decide that the database won’t be so large and that they don’t want to regenerate the database in case the application gets restored from a backup.

}

NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:cachePath error:NULL];
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:self.databasePath error:NULL];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When run from within a unit test bundle before iOS SDK 12.2, -cacheURLIncludingSubdirectory: happened to return a stable result only because of a special case in +[NSBundle mgl_applicationBundleIdentifier]. Now we can rely on the fact that databasePath is memoized.

1ec5 added 2 commits May 5, 2020 22:15
Added getters for the path and URL of the offline database file and made use of the getter in places where the code had previously called -cacheURLIncludingSubdirectory: (which technically could’ve returned a different path each time it was called).

Also reformatted some MGLOfflineStorage documentation comments.
@1ec5 1ec5 force-pushed the 1ec5-offline-db-path-297 branch from 696a99d to ceb5bce Compare May 6, 2020 05:15
Copy link
Contributor

@fabian-guerra fabian-guerra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

@1ec5 1ec5 merged commit 9e53135 into master May 6, 2020
@1ec5 1ec5 deleted the 1ec5-offline-db-path-297 branch May 6, 2020 17:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request ios macOS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add global option to specify offline database path
2 participants