diff options
author | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-09-07 16:18:07 -0700 |
---|---|---|
committer | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-09-13 15:42:02 -0700 |
commit | 448a56e5a671687fe6260350633f2c7b0df9fcc9 (patch) | |
tree | 37e0e39af511a10ccc9c81d53d9dd7ce637acace | |
parent | 3753b4859ab389be64577558fa622e6bf4e10525 (diff) | |
download | qtlocation-mapboxgl-448a56e5a671687fe6260350633f2c7b0df9fcc9.tar.gz |
[ios, macos] Fix refreshing the offline packs after new content is added.
-rw-r--r-- | platform/darwin/src/MGLOfflineStorage.h | 43 | ||||
-rw-r--r-- | platform/darwin/src/MGLOfflineStorage.mm | 34 | ||||
-rw-r--r-- | platform/darwin/test/MGLOfflineStorageTests.mm | 51 | ||||
-rw-r--r-- | platform/ios/CHANGELOG.md | 2 | ||||
-rw-r--r-- | platform/macos/CHANGELOG.md | 2 |
5 files changed, 118 insertions, 14 deletions
diff --git a/platform/darwin/src/MGLOfflineStorage.h b/platform/darwin/src/MGLOfflineStorage.h index 99c14afe52..c9e0ec6426 100644 --- a/platform/darwin/src/MGLOfflineStorage.h +++ b/platform/darwin/src/MGLOfflineStorage.h @@ -137,12 +137,10 @@ typedef void (^MGLOfflinePackRemovalCompletionHandler)(NSError * _Nullable error /** A block to be called once the contents of a file are copied into the current packs. - @param packs Contains an array of all known offline packs, or `nil` if there was - an error creating or adding the pack. @param error Contains a pointer to an error object (if any) indicating why the pack could not be created or added. */ -typedef void (^MGLOfflinePacksAdditionCompletionHandler)(NSArray<MGLOfflinePack *> *packs, NSError * _Nullable error); +typedef void (^MGLOfflinePacksAdditionCompletionHandler)(NSError * _Nullable error); /** The type of resource that is requested. @@ -187,17 +185,44 @@ MGL_EXPORT #pragma mark - Adding Contents of File /** - Adds the contents of the file path. + Adds the offline packs located at the given file path to offline storage. - Once the content is added, then the `completion` block is executed with the - packs passed in. + The file must be a valid offline region database bundled with the application + or downloaded separately. + + Refer to this <a href="https://www.mapbox.com/ios-sdk/maps/examples/offline-pack/">example</a> + to learn how to create additional offline packs. + + The resulting packs are added or updated to the shared offline storage object’s `packs` + property, then the `completion` block is executed. @param filePath A string representation of the file path. The file path must be writable as schema updates may be perfomed. - @param completion The completion handler to call once the file has been added. - This handler is executed asynchronously on the main queue. + @param completion The completion handler to call once the contents of the given + file has been added to offline storage. This handler is executed asynchronously + on the main queue. + */ +- (void)addContentsOfFile:(NSString *)filePath withCompletionHandler:(nullable MGLOfflinePacksAdditionCompletionHandler)completion; + +/** + Adds the offline packs located at the given URL to offline storage. + + The file must be a valid offline region database bundled with the application + or downloaded separately. + + Refer to this <a href="https://www.mapbox.com/ios-sdk/maps/examples/offline-pack/">example</a> + to learn how to create additional offline packs. + + The resulting packs are added or updated to the shared offline storage object’s `packs` + property, then the `completion` block is executed. + + @param fileURL An URL representation of the file. The file URL must be + writable as schema updates may be perfomed. + @param completion The completion handler to call once the contents of the given + file has been added to offline storage. This handler is executed asynchronously + on the main queue. */ -- (void)addContentesOfFile:(NSString *)filePath withCompletionHandler:(nullable MGLOfflinePacksAdditionCompletionHandler)completion; +- (void)addContentsOfURL:(NSURL *)fileURL withCompletionHandler:(nullable MGLOfflinePacksAdditionCompletionHandler)completion; #pragma mark - Accessing the Delegate diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index 616cce160e..0d296f36a5 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -266,9 +266,37 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio #pragma mark Offline merge methods -- (void)addContentesOfFile:(NSString *)filePath withCompletionHandler:(MGLOfflinePacksAdditionCompletionHandler)completion { +- (void)addContentsOfFile:(NSString *)filePath withCompletionHandler:(MGLOfflinePacksAdditionCompletionHandler)completion { NSFileManager *fileManager = [NSFileManager defaultManager]; - NSAssert([fileManager isWritableFileAtPath:filePath], @"The file %@ must be writable.", filePath); + if (![fileManager isWritableFileAtPath:filePath]) { + [NSException raise:NSInvalidArgumentException format:@"The file path must be writable"]; + } + + __weak MGLOfflineStorage *weakSelf = self; + [self _addContentsOfFile:filePath withCompletionHandler:^(NSError * _Nullable error) { + if (error && completion) { + completion(error); + } else { + MGLOfflineStorage *strongSelf = weakSelf; + [strongSelf getPacksWithCompletionHandler:^(NSArray<MGLOfflinePack *> *packs, NSError * _Nullable error) { + for (MGLOfflinePack *pack in strongSelf.packs) { + [pack invalidate]; + } + strongSelf.packs = [packs mutableCopy]; + if (completion) { + completion(error); + } + }]; + } + + }]; +} + +- (void)addContentsOfURL:(NSURL *)fileURL withCompletionHandler:(MGLOfflinePacksAdditionCompletionHandler)completion { + [self addContentsOfFile:fileURL.absoluteString withCompletionHandler:completion]; +} + +- (void)_addContentsOfFile:(NSString *)filePath withCompletionHandler:(MGLOfflinePacksAdditionCompletionHandler)completion { self.mbglFileSource->mergeOfflineRegions(std::string(static_cast<const char *>([filePath UTF8String])), [&, completion](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) { NSError *error; NSMutableArray *packs; @@ -286,7 +314,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio } if (completion) { dispatch_async(dispatch_get_main_queue(), [&, completion, error, packs](void) { - completion(packs, error); + completion(error); }); } }); diff --git a/platform/darwin/test/MGLOfflineStorageTests.mm b/platform/darwin/test/MGLOfflineStorageTests.mm index e9e2467f21..67f129d2ca 100644 --- a/platform/darwin/test/MGLOfflineStorageTests.mm +++ b/platform/darwin/test/MGLOfflineStorageTests.mm @@ -264,4 +264,55 @@ [os setDelegate:nil]; } +- (void)testAddingFileContent { + + NSURL *styleURL = [MGLStyle streetsStyleURLWithVersion:MGLStyleDefaultVersion]; + + // Barcelona. + MGLCoordinateBounds bounds = { + { .latitude = 41.2724, .longitude = 2.0407 }, + { .latitude = 41.4664, .longitude = 2.2680 }, + }; + double zoomLevel = 20; + MGLTilePyramidOfflineRegion *region = [[MGLTilePyramidOfflineRegion alloc] initWithStyleURL:styleURL bounds:bounds fromZoomLevel:zoomLevel toZoomLevel:zoomLevel]; + + NSString *nameKey = @"Name"; + NSString *name = @"Barcelona"; + + NSData *context = [NSKeyedArchiver archivedDataWithRootObject:@{ + nameKey: name, + }]; + + + XCTestExpectation *additionCompletionHandlerExpectation = [self expectationWithDescription:@"add pack completion handler"]; + [[MGLOfflineStorage sharedOfflineStorage] addPackForRegion:region withContext:context completionHandler:^(MGLOfflinePack * _Nullable completionHandlerPack, NSError * _Nullable error) { + XCTAssertNotNil(completionHandlerPack, @"Added pack should exist."); + XCTAssertEqual(completionHandlerPack.state, MGLOfflinePackStateInactive, @"New pack should initially have inactive state."); + [additionCompletionHandlerExpectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:2 handler:nil]; + + NSURL *resourceURL = [NSURL fileURLWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"barcelona" ofType:@"db"]]; + NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); + NSString *documentDir = [documentPaths objectAtIndex:0]; + NSString *filePath = [documentDir stringByAppendingPathComponent:@"barcelona.db"]; + NSFileManager *fileManager = [NSFileManager defaultManager]; + + BOOL exists = [fileManager fileExistsAtPath:filePath]; + if (exists) { + [fileManager removeItemAtPath:filePath error:nil]; + } + + NSError *error; + [fileManager moveItemAtURL:resourceURL toURL:[NSURL fileURLWithPath:filePath] error:&error]; + + XCTestExpectation *fileAdditionCompletionHandlerExpectation = [self expectationWithDescription:@"add database content completion handler"]; + MGLOfflineStorage *os = [MGLOfflineStorage sharedOfflineStorage]; + [os addContentsOfFile:filePath withCompletionHandler:^(NSError * _Nullable error) { + XCTAssertNil(error, @"Adding contents to a file should not return an error."); + [fileAdditionCompletionHandlerExpectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:2 handler:nil]; +} + @end diff --git a/platform/ios/CHANGELOG.md b/platform/ios/CHANGELOG.md index f3e578f3f3..15b168fd1d 100644 --- a/platform/ios/CHANGELOG.md +++ b/platform/ios/CHANGELOG.md @@ -31,7 +31,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT * Added the `MGLShapeOfflineRegion` class for creating an offline pack that covers an arbitrary shape. ([#11447](https://github.com/mapbox/mapbox-gl-native/pull/11447)) * Fixed crashes when offline storage encountered certain SQLite errors. ([#12224](https://github.com/mapbox/mapbox-gl-native/pull/12224)) -* Added an `-[MGLOfflineStorage addContentesOfFile:withCompletionHandler:]` method to add the content of a database into the main offline database. ([#12791](https://github.com/mapbox/mapbox-gl-native/pull/12791)) +* Added an `-[MGLOfflineStorage addContentesOfFile:withCompletionHandler:]` method to add pregenerated offline packs to offline storage. ([#12791](https://github.com/mapbox/mapbox-gl-native/pull/12791)) ### Other changes diff --git a/platform/macos/CHANGELOG.md b/platform/macos/CHANGELOG.md index cea816537c..5fb4d1bc00 100644 --- a/platform/macos/CHANGELOG.md +++ b/platform/macos/CHANGELOG.md @@ -29,7 +29,7 @@ * Added the `MGLShapeOfflineRegion` class for creating an offline pack that covers an arbitrary shape. ([#11447](https://github.com/mapbox/mapbox-gl-native/pull/11447)) * Fixed crashes when offline storage encountered certain SQLite errors. ([#12224](https://github.com/mapbox/mapbox-gl-native/pull/12224)) -* Added an `-[MGLOfflineStorage addContentesOfFile:withCompletionHandler:]` method to add the content of a database into the main offline database. ([#12791](https://github.com/mapbox/mapbox-gl-native/pull/12791)) +* Added an `-[MGLOfflineStorage addContentesOfFile:withCompletionHandler:]` method to add pregenerated offline packs to offline storage. ([#12791](https://github.com/mapbox/mapbox-gl-native/pull/12791)) ### Other changes |