From 3b546b964609d0f596dac32e155b1489bb85645e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Sat, 27 Aug 2016 15:56:19 -0700 Subject: [PATCH] [ios, macos] Ignore progress updates after suspending -suspend and -resume are now presumed to be synchronous, even if technically they remain asynchronous under the hood. --- platform/darwin/src/MGLOfflinePack.h | 12 +++--------- platform/darwin/src/MGLOfflinePack.mm | 20 ++++++++++++++++++-- platform/ios/CHANGELOG.md | 1 + platform/macos/CHANGELOG.md | 1 + 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/platform/darwin/src/MGLOfflinePack.h b/platform/darwin/src/MGLOfflinePack.h index 3d4422da3..a14d001d0 100644 --- a/platform/darwin/src/MGLOfflinePack.h +++ b/platform/darwin/src/MGLOfflinePack.h @@ -144,11 +144,6 @@ typedef struct MGLOfflinePackProgress { /** Resumes downloading if the pack is inactive. - A pack resumes asynchronously. To get notified when this pack resumes, observe - KVO change notifications on this pack’s `state` key path. Alternatively, you - can add an observer for `MGLOfflinePackProgressChangedNotification`s about this - pack that come from the default notification center. - When a pack resumes after being suspended, it may begin by iterating over the already downloaded resources. As a result, the `progress` structure’s `countOfResourcesCompleted` field may revert to 0 before rapidly returning to @@ -161,10 +156,9 @@ typedef struct MGLOfflinePackProgress { /** Temporarily stops downloading if the pack is active. - A pack suspends asynchronously. To get notified when this pack resumes, observe - KVO change notifications on this pack’s `state` key path. Alternatively, you - can add an observer for `MGLOfflinePackProgressChangedNotification` about this - pack that come from the default notification center. + A pack suspends asynchronously, so some network requests may be sent after this + method is called. Regardless, the `progress` property will not be updated until + `-resume` is called. If the pack previously reached a higher level of progress before being suspended, it may wait to suspend until it returns to that level. diff --git a/platform/darwin/src/MGLOfflinePack.mm b/platform/darwin/src/MGLOfflinePack.mm index 59406074e..1f2fd95f2 100644 --- a/platform/darwin/src/MGLOfflinePack.mm +++ b/platform/darwin/src/MGLOfflinePack.mm @@ -43,7 +43,9 @@ private: @end -@implementation MGLOfflinePack +@implementation MGLOfflinePack { + BOOL _isSuspending; +} - (instancetype)init { if (self = [super init]) { @@ -86,6 +88,8 @@ private: - (void)resume { MGLAssertOfflinePackIsValid(); + self.state = MGLOfflinePackStateActive; + mbgl::DefaultFileSource *mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; mbglFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Active); } @@ -93,6 +97,11 @@ private: - (void)suspend { MGLAssertOfflinePackIsValid(); + if (self.state == MGLOfflinePackStateActive) { + self.state = MGLOfflinePackStateInactive; + _isSuspending = YES; + } + mbgl::DefaultFileSource *mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; mbglFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Inactive); } @@ -117,7 +126,10 @@ private: NSAssert(_state != MGLOfflinePackStateInvalid, @"Cannot change the state of an invalid offline pack."); - _state = state; + if (!_isSuspending || state != MGLOfflinePackStateActive) { + _isSuspending = NO; + _state = state; + } } - (void)requestProgress { @@ -150,6 +162,10 @@ private: break; } + if (_isSuspending) { + return; + } + MGLOfflinePackProgress progress; progress.countOfResourcesCompleted = status.completedResourceCount; progress.countOfBytesCompleted = status.completedResourceSize; diff --git a/platform/ios/CHANGELOG.md b/platform/ios/CHANGELOG.md index f1b869dc4..3cef67ca7 100644 --- a/platform/ios/CHANGELOG.md +++ b/platform/ios/CHANGELOG.md @@ -49,6 +49,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT * Fixed a crash that sometimes occurred when initializing an MGLMapView. ([#5932](https://github.com/mapbox/mapbox-gl-native/pull/5932)) * As the user zooms in, tiles from lower zoom levels are scaled up until tiles for higher zoom levels are loaded. ([#5143](https://github.com/mapbox/mapbox-gl-native/pull/5143)) +* Fixed an issue causing an MGLOfflinePack’s progress to continue to update after calling `-suspend`. ([#6186](https://github.com/mapbox/mapbox-gl-native/pull/6186)) * MGLMapDebugOverdrawVisualizationMask no longer has any effect in Release builds of the SDK. This debug mask has been disabled for performance reasons. ([#5555](https://github.com/mapbox/mapbox-gl-native/pull/5555)) * Fixed a typo in the documentation for the MGLCompassDirectionFormatter class. ([#5879](https://github.com/mapbox/mapbox-gl-native/pull/5879)) diff --git a/platform/macos/CHANGELOG.md b/platform/macos/CHANGELOG.md index fd8a6f31d..4f7d6f15b 100644 --- a/platform/macos/CHANGELOG.md +++ b/platform/macos/CHANGELOG.md @@ -14,6 +14,7 @@ * Fixed rendering artifacts and missing glyphs that occurred after viewing a large number of CJK characters on the map. ([#5908](https://github.com/mapbox/mapbox-gl-native/pull/5908)) * Improved the precision of annotations at zoom levels greater than 18. ([#5517](https://github.com/mapbox/mapbox-gl-native/pull/5517)) * Fixed an issue where the style zoom levels were not respected when deciding when to render a layer. ([#5811](https://github.com/mapbox/mapbox-gl-native/issues/5811)) +* Fixed an issue causing an MGLOfflinePack’s progress to continue to update after calling `-suspend`. ([#6186](https://github.com/mapbox/mapbox-gl-native/pull/6186)) * If MGLMapView is unable to obtain or parse a style, it now calls its delegate’s `-mapViewDidFailLoadingMap:withError:` method. ([#6145](https://github.com/mapbox/mapbox-gl-native/pull/6145)) * Fixed crashes that could occur when loading a malformed stylesheet. ([#5736](https://github.com/mapbox/mapbox-gl-native/pull/5736)) * Fixed a typo in the documentation for the MGLCompassDirectionFormatter class. ([#5879](https://github.com/mapbox/mapbox-gl-native/pull/5879))