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

Commit

Permalink
[ios, macos] Fix offline packs, updating triggers a crash.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-guerra committed Sep 17, 2018
1 parent a5142d6 commit 2478c59
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
14 changes: 4 additions & 10 deletions platform/darwin/src/MGLOfflineStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ typedef void (^MGLOfflinePackRemovalCompletionHandler)(NSError * _Nullable error
@param fileURL The file URL of the offline database containing the offline packs
that were copied.
@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.
@param packs An array of all known offline packs, or `nil` if there was an error
creating or adding the pack.
@param error A pointer to an error object (if any) indicating why the pack could
not be created or added.
*/
typedef void (^MGLBatchedOfflinePackAdditionCompletionHandler)(NSURL *fileURL, NSArray<MGLOfflinePack *> * _Nullable packs, NSError * _Nullable error);

Expand Down Expand Up @@ -194,9 +194,6 @@ MGL_EXPORT
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 download offline packs.
The resulting packs are added or updated to the shared offline storage object’s `packs`
property, then the `completion` block is executed.
Expand All @@ -214,9 +211,6 @@ MGL_EXPORT
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 download offline packs.
The resulting packs are added or updated to the shared offline storage object’s `packs`
property, then the `completion` block is executed.
Expand Down
4 changes: 3 additions & 1 deletion platform/darwin/src/MGLOfflineStorage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -296,19 +296,21 @@ - (void)addContentsOfURL:(NSURL *)fileURL withCompletionHandler:(MGLBatchedOffli

id mutablePacks = [strongSelf mutableArrayValueForKey:@"packs"];
NSMutableIndexSet *replaceIndexSet = [NSMutableIndexSet indexSet];
NSMutableArray *replacePacksArray = [NSMutableArray array];
[strongSelf.packs enumerateObjectsUsingBlock:^(MGLOfflinePack * _Nonnull pack, NSUInteger idx, BOOL * _Nonnull stop) {
MGLOfflinePack *newPack = packsByIdentifier[@(pack.mbglOfflineRegion->getID())];
if (newPack) {
MGLOfflinePack *previousPack = [mutablePacks objectAtIndex:idx];
[previousPack invalidate];
[replaceIndexSet addIndex:idx];
[replacePacksArray addObject:[packsByIdentifier objectForKey:@(newPack.mbglOfflineRegion->getID())]];
[packsByIdentifier removeObjectForKey:@(newPack.mbglOfflineRegion->getID())];
}

}];

if (replaceIndexSet.count > 0) {
[mutablePacks replaceObjectsAtIndexes:replaceIndexSet withObjects:packs];
[mutablePacks replaceObjectsAtIndexes:replaceIndexSet withObjects:replacePacksArray];
}

[mutablePacks addObjectsFromArray:packsByIdentifier.allValues];
Expand Down
5 changes: 5 additions & 0 deletions platform/darwin/test/MGLOfflineStorageTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ - (void)testAddFileContent {

NSUInteger countOfPacks = [MGLOfflineStorage sharedOfflineStorage].packs.count;

[self keyValueObservingExpectationForObject:[MGLOfflineStorage sharedOfflineStorage] keyPath:@"packs" handler:^BOOL(id _Nonnull observedObject, NSDictionary * _Nonnull change) {
const auto changeKind = static_cast<NSKeyValueChange>([change[NSKeyValueChangeKindKey] unsignedLongValue]);
NSIndexSet *indices = change[NSKeyValueChangeIndexesKey];
return changeKind == NSKeyValueChangeInsertion && indices.count == 1;
}];

XCTestExpectation *fileAdditionCompletionHandlerExpectation = [self expectationWithDescription:@"add database content completion handler"];
MGLOfflineStorage *os = [MGLOfflineStorage sharedOfflineStorage];
Expand Down

0 comments on commit 2478c59

Please sign in to comment.