diff options
author | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-09-13 12:27:12 -0700 |
---|---|---|
committer | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-09-13 15:42:02 -0700 |
commit | 14ba3932123e6a2c25843756ec372388ffc61b89 (patch) | |
tree | 33af2d58a5a53bbb96c3c07249cbbfdf442fc573 | |
parent | fb1aa4d6ee760188dbd0d53884c50ec77be8e9d7 (diff) | |
download | qtlocation-mapboxgl-14ba3932123e6a2c25843756ec372388ffc61b89.tar.gz |
[ios, macos] Remove unnecessary pack iteration.
# Conflicts:
# platform/darwin/src/MGLOfflineStorage.h
# platform/darwin/src/MGLOfflineStorage.mm
-rw-r--r-- | platform/darwin/src/MGLOfflineStorage.h | 3 | ||||
-rw-r--r-- | platform/darwin/src/MGLOfflineStorage.mm | 37 |
2 files changed, 21 insertions, 19 deletions
diff --git a/platform/darwin/src/MGLOfflineStorage.h b/platform/darwin/src/MGLOfflineStorage.h index 1ea8cece07..e14b3dfe51 100644 --- a/platform/darwin/src/MGLOfflineStorage.h +++ b/platform/darwin/src/MGLOfflineStorage.h @@ -137,7 +137,8 @@ typedef void (^MGLOfflinePackRemovalCompletionHandler)(NSError * _Nullable error /** A block to be called once the contents of a file are copied into the current packs. - @param fileURL Contains the URL to the file packs used. + @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 diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index 7b10f869a6..f6ec28a7ce 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -287,27 +287,26 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio __weak MGLOfflineStorage *weakSelf = self; [self _addContentsOfFile:fileURL.path withCompletionHandler:^(NSArray<MGLOfflinePack *> * _Nullable packs, NSError * _Nullable error) { if (packs) { - NSMutableDictionary *packsDictionary = [NSMutableDictionary dictionary]; - NSMutableDictionary *packsIndex = [NSMutableDictionary dictionary]; - NSInteger index = 0; + NSMutableDictionary *packsByIdentifier = [NSMutableDictionary dictionary]; MGLOfflineStorage *strongSelf = weakSelf; - for (MGLOfflinePack *pack in strongSelf.packs) { - [packsDictionary setObject:pack forKey:@(pack.mbglOfflineRegion->getID())]; - [packsIndex setObject:@(index) forKey:@(pack.mbglOfflineRegion->getID())]; - index++; + for (MGLOfflinePack *pack in packs) { + [packsByIdentifier setObject:pack forKey:@(pack.mbglOfflineRegion->getID())]; } - for (MGLOfflinePack *pack in packs) { - MGLOfflinePack *existingPack = [packsDictionary objectForKey:@(pack.mbglOfflineRegion->getID())]; - if (existingPack) { - [existingPack invalidate]; - NSNumber *index = [packsIndex objectForKey:@(pack.mbglOfflineRegion->getID())]; - [[strongSelf mutableArrayValueForKey:@"packs"] replaceObjectAtIndex:index.unsignedIntegerValue withObject:pack]; - } else { - [[strongSelf mutableArrayValueForKey:@"packs"] addObject:pack]; + id mutablePacks = [strongSelf mutableArrayValueForKey:@"packs"]; + [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]; + [mutablePacks replaceObjectAtIndex:idx withObject:newPack]; + packsByIdentifier[@(newPack.mbglOfflineRegion->getID())] = nil; } - } + + }]; + + [mutablePacks addObjectsFromArray:packsByIdentifier.allValues]; } if (completion) { completion(fileURL, packs, error); @@ -316,12 +315,14 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio } - (void)_addContentsOfFile:(NSString *)filePath withCompletionHandler:(void (^)(NSArray<MGLOfflinePack *> * _Nullable packs, NSError * _Nullable error))completion { - self.mbglFileSource->mergeOfflineRegions(std::string(static_cast<const char *>([filePath UTF8String])), [&, completion](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) { + self.mbglFileSource->mergeOfflineRegions(std::string(static_cast<const char *>([filePath UTF8String])), [&, completion, filePath](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) { NSError *error; NSMutableArray *packs; if (!result) { + NSString *description = [NSString stringWithFormat:NSLocalizedStringWithDefaultValue(@"ADD_FILE_CONTENTS_FAILED_DESC", nil, nil, @"Unable to add offline packs from the file at %@.", @"User-friendly error description"), filePath]; error = [NSError errorWithDomain:MGLErrorDomain code:-1 userInfo:@{ - NSLocalizedDescriptionKey: @(mbgl::util::toString(result.error()).c_str()), + NSLocalizedDescriptionKey: description, + NSLocalizedFailureReasonErrorKey: @(mbgl::util::toString(result.error()).c_str()) }]; } else { auto& regions = result.value(); |