summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjmkiley <jordan.kiley@mapbox.com>2019-06-27 11:39:01 -0700
committerjmkiley <jordan.kiley@mapbox.com>2019-07-09 15:03:39 -0700
commita1027e93c618926ec680e0086ed7909ddb643bc8 (patch)
tree986a57723b9724d25e09abe13b2e58080936f75d
parent9fc03e461dbfdd281d6dd231cdc3ee1786ab5cd9 (diff)
downloadqtlocation-mapboxgl-a1027e93c618926ec680e0086ed7909ddb643bc8.tar.gz
[ios] add resetDatabase implementation and example
-rw-r--r--platform/darwin/src/MGLOfflineStorage.h5
-rw-r--r--platform/darwin/src/MGLOfflineStorage.mm17
-rw-r--r--platform/ios/app/MBXViewController.m45
3 files changed, 46 insertions, 21 deletions
diff --git a/platform/darwin/src/MGLOfflineStorage.h b/platform/darwin/src/MGLOfflineStorage.h
index bb8eff6c88..0b355e2fc6 100644
--- a/platform/darwin/src/MGLOfflineStorage.h
+++ b/platform/darwin/src/MGLOfflineStorage.h
@@ -347,10 +347,11 @@ MGL_EXPORT
// JK FINISH DOC
/*
- Sets the maximum ambient cache size in megabytes. The default maximum cache size is _ MB. To disable ambient caching, set the maximum ambient cache size to 0.
+ Sets the maximum ambient cache size in megabytes. The default maximum cache size is _ MB. To disable ambient caching, set the maximum ambient cache size to 0. Setting the maximum ambient cache size does not impact the maximum size for offline packs.
This method should be called before using the database.
*/
+
- (void)setMaximumAmbientCacheSize:(NSInteger)cacheSize withCallback:(void (^)(NSError *_Nullable error))completion;
/*
@@ -361,6 +362,8 @@ MGL_EXPORT
- (void)clearAmbientCacheWithCompletion:(void (^)(NSError *_Nullable error))completion;
+- (void)resetDatabaseWithCompletionHandler:(void (^)(NSError *_Nullable error))completion;
+
/*
Inserts the provided resource into the ambient cache.
diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm
index 72d69a1eeb..b91da5b26c 100644
--- a/platform/darwin/src/MGLOfflineStorage.mm
+++ b/platform/darwin/src/MGLOfflineStorage.mm
@@ -553,6 +553,23 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio
});
});
}
+
+- (void)resetDatabaseWithCompletionHandler:(void (^)(NSError *_Nullable error))completion {
+ if (!completion) { return; };
+ NSError *error;
+ _mbglFileSource->resetDatabase([&, completion](std::exception_ptr exception) {
+ if (exception) {
+ error = [NSError errorWithDomain:MGLErrorDomain
+ code:-1 userInfo:@{
+ NSLocalizedDescriptionKey: @(mbgl::util::toString(exception).c_str()),
+ }];
+ }
+ dispatch_async(dispatch_get_main_queue(), [&, completion, error](void) {
+ completion(error);
+ });
+
+ });
+}
#pragma mark -
- (unsigned long long)countOfBytesCompleted {
diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m
index f774b7f5bc..1ca55fe34f 100644
--- a/platform/ios/app/MBXViewController.m
+++ b/platform/ios/app/MBXViewController.m
@@ -1787,40 +1787,44 @@ CLLocationCoordinate2D randomWorldCoordinate() {
// Getting a bad access exception
- (void)invalidateAmbientCache {
- __block NSString *path = [self getCachePath];
- NSFileManager *manager = [NSFileManager defaultManager];
- unsigned long long fileSize = [[manager attributesOfItemAtPath:path error:nil] fileSize];
- NSLog(@"CACHE SIZE: %llu", fileSize);
-
+ unsigned long long fileSize = [self getCacheSize];
+ NSLog(@"STARTING WITH CACHE SIZE: %llu", fileSize);
[[MGLOfflineStorage sharedOfflineStorage] invalidateAmbientCacheWithCompletion:^(NSError * _Nullable error) {
- NSLog(@"Ambient cache has been reloaded!");
- path = [self getCachePath];
- unsigned long long newFileSize = [[manager attributesOfItemAtPath:path error:nil] fileSize];
- NSLog(@"CACHE SIZE: %llu", newFileSize);
+ unsigned long long newFileSize = [self getCacheSize];
+ NSLog(@"\nAmbient cache has been reloaded!CACHE SIZE: %llu", newFileSize);
}];
}
- (void)resetDatabase {
-
+ CFTimeInterval start = CACurrentMediaTime();
+ [[MGLOfflineStorage sharedOfflineStorage] resetDatabaseWithCompletionHandler:^(NSError * _Nullable error) {
+ if (!error) {
+ CFTimeInterval end = CACurrentMediaTime();
+ CFTimeInterval difference = end - start;
+ NSLog(@"clearDatabase\nStarted: %f\nEnded: %f\nTotal Time: %f", start, end, difference);
+ }
+ }];
}
- (void)clearAmbientCache {
// Access the cache.db file
- __block NSString *path = [self getCachePath];
- NSFileManager *manager = [NSFileManager defaultManager];
- unsigned long long fileSize = [[manager attributesOfItemAtPath:path error:nil] fileSize];
+ unsigned long long fileSize = [self getCacheSize];
NSLog(@"CACHE SIZE: %llu", fileSize);
+ CFTimeInterval start = CACurrentMediaTime();
[[MGLOfflineStorage sharedOfflineStorage] clearAmbientCacheWithCompletion:^(NSError * _Nullable error) {
- NSLog(@"DONE");
- path = [self getCachePath];
- unsigned long long newFileSize = [[manager attributesOfItemAtPath:path error:nil] fileSize];
+ unsigned long long newFileSize = [self getCacheSize];
NSLog(@"CACHE SIZE: %llu", newFileSize);
+ CFTimeInterval end = CACurrentMediaTime();
+ CFTimeInterval difference = end - start;
+ NSLog(@"clearAmbientCache\nStarted: %f\nEnded: %f\nTotal Time: %f", start, end, difference);
}];
}
-// This method is used to access the cache database file.
-- (NSString *)getCachePath {
- NSURL *cacheDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory
+// This method is used to access the cache database file, then get the file size.
+- (unsigned long long)getCacheSize {
+
+ NSFileManager *manager = [NSFileManager defaultManager];
+ NSURL *cacheDirectoryURL = [manager URLForDirectory:NSApplicationSupportDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:YES
@@ -1832,7 +1836,8 @@ CLLocationCoordinate2D randomWorldCoordinate() {
cacheDirectoryURL = [cacheDirectoryURL URLByAppendingPathComponent:bundleIdentifier];
cacheDirectoryURL = [cacheDirectoryURL URLByAppendingPathComponent:@".mapbox/cache.db"];
// cacheDirectoryURL = [cacheDirectoryURL URLByAppendingPathComponent:@""];
- return cacheDirectoryURL.path;
+ NSString *path = cacheDirectoryURL.path;
+ return [[manager attributesOfItemAtPath:path error:nil] fileSize];
}
#pragma mark - Random World Tour