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

Centralize offline pack management with KVO compliance and tests - #4482

Merged
1ec5 merged 13 commits into
release-ios-3.2.0-android-4.0.0from
1ec5-offline-kvo-4287
Mar 29, 2016
Merged

1ec5 merged 13 commits into
release-ios-3.2.0-android-4.0.0from
1ec5-offline-kvo-4287

Conversation

@1ec5

@1ec5 1ec5 commented Mar 27, 2016

Copy link
Copy Markdown
Contributor

MGLOfflineStorage now maintains a canonical, strongly held collection of all valid offline packs and issues notifications for progress updates. MGLOfflinePackDelegate is now private and serves only as a link between MGLOfflineStorage and its MGLOfflinePacks. Clients that want to receive progress updates should use NSNotificationCenter or key-value observing (KVO) instead of the delegate pattern. Progress updates come automatically: it’s no longer necessary (or possible) to call -requestProgress on an offline pack.

Client code can react to the successful addition of an offline pack either via the completion block or via a KVO insertion notification. Client code can get a list of packs synchronously, although the list is still populated asynchronously from the time MGLOfflineStorage or an MGLMapView is first initialized. The old -getPacksWithCompletionHandler: method is replaced with a -reloadPacks method for one edge case detailed in the documentation.

A battery of automated functional tests has been added for the MGLTilePyramidOfflineRegion, MGLOfflinePack, and MGLOfflineStorage classes. They test essential aspects of pack management and state changes but don’t test actual downloading; that functionality is best covered by mbgl command line tests for now. The NS_UNAVAILABLE attribute has been removed from -[MGLOfflinePack init] to facilitate testing. Packs initialized this way are doomed to invalidity, as they are disconnected from the shared MGLOfflineStorage object.

Even though an added pack’s region has a different pointer value than the region that’s passed into -[MGLOfflineStorage addPackForRegion:withContext:completionHandler:], the two regions are equal by object equality, now that MGLTilePyramidOfflineRegion overrides -isEqual:. It also conforms to both NSSecureCoding and NSCopying.

This PR also comes with some improvements to iosapp and osxapp:

  • Previously, if the user added an offline pack without specifying a name in the alert, the pack would be named “Untitled n” for the nth pack created since the application was launched. This logic could result in identically named packs (for example, if the user relaunches and creates another unnamed pack). Instead, use the visible coordinate bounds as a placeholder and fallback name.
  • When possible, iosapp updates individual labels in a cell instead of reloading the entire cell. That way, if the user swipes to the left to reveal the Delete accessory button while the pack is actively downloading, the accessory button remains on screen as progress updates come in.
  • osxapp has a new offline pack download manager that demonstrates the use of MGLOfflineStorage with Cocoa Bindings. Like the Offline Packs view controller in iosapp, new packs are added based on the foremost map view’s current state. Double-clicking a complete offline pack opens a new map window to the pack’s bounds.

Fixes #4287, fixes #4394, fixes #4421, and fixes #4426.

/cc @jfirebaugh @boundsj @friedbunny @bsudekum

@1ec5 1ec5 self-assigned this Mar 27, 2016
@1ec5 1ec5 added iOS Mapbox Maps SDK for iOS refactor macOS Mapbox Maps SDK for macOS crash tests offline labels Mar 27, 2016
@1ec5 1ec5 added this to the ios-v3.2.0 milestone Mar 27, 2016
@1ec5

1ec5 commented Mar 27, 2016

Copy link
Copy Markdown
Contributor Author

The new design relies pretty heavily on KVO and notifications, both of which are pretty powerful but not the easiest technologies for less experienced programmers to grok. We’ll need to do a bit more hand-holding in documentation for this to work well. On the OS X side, at least it plays nicely with Cocoa Bindings…

@jfirebaugh

Copy link
Copy Markdown
Contributor

Under what circumstances does this implementation call DefaultFileSource::getOfflineRegionStatus? E.g. does it call it essentially whenever an offline pack is created or listed?

@1ec5

1ec5 commented Mar 28, 2016

Copy link
Copy Markdown
Contributor Author

Under what circumstances does this implementation call DefaultFileSource::getOfflineRegionStatus? E.g. does it call it essentially whenever an offline pack is created or listed?

Yes, via -[MGLOfflinePack requestProgress]. Additionally, MGLOfflinePack observes progress updates for its entire lifetime, ensuring that getOfflineRegionStatus() doesn’t need to be called explicitly after the initial request to -requestProgress for active mbgl::OfflineRegions. This is the only way I could find to give clients synchronous access to the state or progress via KVO.

@jfirebaugh

Copy link
Copy Markdown
Contributor

😬 That makes listing an O(n^2) operation.

@1ec5

1ec5 commented Mar 28, 2016

Copy link
Copy Markdown
Contributor Author

-[MGLOfflinePack requestProgress] could be made public again and MGLOfflineStorage would avoid calling it. But if MGLOfflineStorage doesn’t automatically request progress for each pack it knows about, it’s likely that client code would call -requestProgress on all the packs anyways, because it’s inconvenient to work with a pack whose status is unknown. The documentation in this PR makes clear that the status is unknown for an undefined amount of time. However, if push comes to shove, I’m willing to make -requestProgress public and explicit again, despite the awkwardness.

In chat, @jfirebaugh suggested that we could obviate the use of DefaultFileSource::getOfflineRegionStatus() and potentially improve performance if DefaultFileSource::listOfflineRegions()’s callback maps mbgl::OfflineRegions to their statuses, possibly guarded by a flag. If getting an mbgl::OfflineRegionDefinition is reasonably cheap, we could also prioritize calling -requestProgress on regions by tile count (estimated based on the zoom levels and bounds).

As a gut check, I measured my offline cache, which currently contains 50 offline packs weighing in at 450 MB total. The list of offline packs took on average 2 ms to load, but the statuses came in serially, so one 429.5 MB pack took on the order of 10 seconds to get its status, at which point osxapp peaked at 630 MB. After that point, the remaining 73.5 MB packs each took about 100 ms to get their statuses, and memory usage fell considerably.

@jfirebaugh

Copy link
Copy Markdown
Contributor

After reviewing the code, I don't see a way to include the status with DefaultFileSource::listOfflineRegions that's feasible for 3.2.0. Calculating the status is a complex operation that involves much of the same machinery as actually downloading the region.

However, if push comes to shove, I’m willing to make -requestProgress public and explicit again, despite the awkwardness.

This is the approach I think we should take for 3.2.0. We can revisit automatic progress computation for a future version. Making it acceptably efficient will likely require database schema changes and a streaming tileCover implementation.

@1ec5

1ec5 commented Mar 29, 2016

Copy link
Copy Markdown
Contributor Author

My previous test was in osxapp.

For my next test, in iosapp on a Wi-Fi connection, I downloaded the entirety of California’s largest metropolitan areas (San José, San Francisco, Oakland, Sacramento, Los Angeles et al., and San Diego) in the Mapbox Streets style, one by one, starting from the lowest zoom level necessary to cover the metropolitan area to the maximum zoom level allowed by the Mapbox Streets source. Memory usage tops out at 193.9 MB and CPU usage tops out at 55%.

When I quite and relaunched iosapp, CPU usage briefly peaked at 63%, while memory usage plateaued each time an OfflineRegion’s status came in, eventually plateauing at 54.9 MB. The application hung at launch for one minute while the packs’ statuses were being retrieved.

Based on these findings, I think we should at the very least defer calling -requestProgress on any pack until the first time client code calls +[MGLOfflineStorage sharedOfflineStorage]. Applications that only use MGLMapView should not incur this penalty.

@1ec5

1ec5 commented Mar 29, 2016

Copy link
Copy Markdown
Contributor Author

Alright, making -requestProgress explicit again is the best we can do then.

@1ec5

1ec5 commented Mar 29, 2016

Copy link
Copy Markdown
Contributor Author

The hang on launch, incidentally, looks like this in iosapp (but doesn’t occur in osxapp):

Thread 1 Queue : com.apple.main-thread (serial)
#5  0x00000001826f4130 in std::__1::future<void>::get() ()
#6  0x00000001006d4ecc in mbgl::util::Thread<mbgl::DefaultFileSource::Impl>::Thread<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long long&>(mbgl::util::ThreadContext const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&&&, unsigned long long&&&) at /path/to/mapbox-gl-native/gyp/../src/mbgl/util/thread.hpp:109
#7  0x00000001006d4ca4 in mbgl::util::Thread<mbgl::DefaultFileSource::Impl>::Thread<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long long&>(mbgl::util::ThreadContext const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&&&, unsigned long long&&&) at /path/to/mapbox-gl-native/gyp/../src/mbgl/util/thread.hpp:90
#8  0x00000001006c61ac in std::__1::__unique_if<mbgl::util::Thread<mbgl::DefaultFileSource::Impl> >::__unique_single std::__1::make_unique<mbgl::util::Thread<mbgl::DefaultFileSource::Impl>, mbgl::util::ThreadContext, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long long&>(mbgl::util::ThreadContext&&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&&&, unsigned long long&&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:3132
#9  0x00000001006c615c in mbgl::DefaultFileSource::DefaultFileSource(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long long) at /path/to/mapbox-gl-native/platform/default/default_file_source.cpp:142
#10 0x00000001006c6760 in mbgl::DefaultFileSource::DefaultFileSource(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long long) at /path/to/mapbox-gl-native/platform/default/default_file_source.cpp:144
#11 0x00000001007535cc in -[MGLOfflineStorage init] at /path/to/mapbox-gl-native/platform/darwin/src/MGLOfflineStorage.mm:87
#12 0x0000000100752ccc in __41+[MGLOfflineStorage sharedOfflineStorage]_block_invoke at /path/to/mapbox-gl-native/platform/darwin/src/MGLOfflineStorage.mm:36
#13 0x0000000100f5da3c in _dispatch_client_callout ()
#14 0x0000000100f5e8b4 in dispatch_once_f ()
#15 0x0000000100752c1c in _dispatch_once(long*, void () block_pointer) [inlined] at /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.3.sdk/usr/include/dispatch/once.h:68
#16 0x0000000100752c00 in +[MGLOfflineStorage sharedOfflineStorage] at /path/to/mapbox-gl-native/platform/darwin/src/MGLOfflineStorage.mm:35
#17 0x000000010076cc18 in -[MGLMapView commonInit] at /path/to/mapbox-gl-native/platform/ios/src/MGLMapView.mm:316
Thread 10
#15 0x00000001835d25d4 in sqlite3_exec ()
#16 0x000000010073ed10 in mapbox::sqlite::Database::exec(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) at /path/to/mapbox-gl-native/platform/default/sqlite3.cpp:74
#17 0x000000010072269c in mbgl::OfflineDatabase::migrateToVersion3() at /path/to/mapbox-gl-native/platform/default/mbgl/storage/offline_database.cpp:113
#18 0x0000000100721488 in mbgl::OfflineDatabase::ensureSchema() at /path/to/mapbox-gl-native/platform/default/mbgl/storage/offline_database.cpp:53
#19 0x0000000100721284 in mbgl::OfflineDatabase::OfflineDatabase(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long long) at /path/to/mapbox-gl-native/platform/default/mbgl/storage/offline_database.cpp:25
#20 0x0000000100721bc0 in mbgl::OfflineDatabase::OfflineDatabase(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long long) at /path/to/mapbox-gl-native/platform/default/mbgl/storage/offline_database.cpp:24
#21 0x00000001006d5bfc in mbgl::DefaultFileSource::Impl::Impl(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long long) at /path/to/mapbox-gl-native/platform/default/default_file_source.cpp:52
#22 0x00000001006d5af8 in mbgl::DefaultFileSource::Impl::Impl(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long long) at /path/to/mapbox-gl-native/platform/default/default_file_source.cpp:52
#23 0x00000001006d5924 in void mbgl::util::Thread<mbgl::DefaultFileSource::Impl>::run<std::__1::tuple<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long long&>, 0ul, 1ul>(mbgl::util::ThreadContext, std::__1::tuple<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long long&>&&, std::__1::integer_sequence<unsigned long, 0ul, 1ul>) at /path/to/mapbox-gl-native/gyp/../src/mbgl/util/thread.hpp:120

@1ec5

1ec5 commented Mar 29, 2016

Copy link
Copy Markdown
Contributor Author

The hang in #4482 (comment) is being tracked in #4501; it doesn’t appear to be specific to the act of requesting progress for all the packs at launch.

@1ec5
1ec5 force-pushed the 1ec5-offline-kvo-4287 branch from 4368f37 to 552c78c Compare March 29, 2016 00:36
@1ec5

1ec5 commented Mar 29, 2016

Copy link
Copy Markdown
Contributor Author

Offline pack progress is once again requested manually by client code, addressing the performance concern above. We should definitely look for a way to eliminate this performance bottleneck in a future release so we can make progress updates transparent to client code.

@boundsj

boundsj commented Mar 29, 2016

Copy link
Copy Markdown
Contributor

👍

@1ec5
1ec5 force-pushed the 1ec5-offline-kvo-4287 branch from 2906cce to 0c197fa Compare March 29, 2016 17:27
@1ec5

1ec5 commented Mar 29, 2016

Copy link
Copy Markdown
Contributor Author

Someone aborted all the builds, but they all passed last night, so I’ll merge as soon as #4504 lands.

MGLOfflineStorage now maintains a centralized, strongly held, KVO-compliant collection of all extant offline packs. MGLOfflineStorage issues notifications for progress updates. Client code can now react to the successful addition of an offline pack either via the completion block or via a KVO insertion.

Fixes #4287.
1ec5 added 12 commits March 29, 2016 11:46
…name, fewer reloads

Made MGLOfflineStorage.packs strong rather than copy so that KVO notifications on that key path include index paths. Replaced custom KVO mutable collection methods with calls to -mutableArrayValueForKey:, avoiding redundant casts.

Privatized MGLOfflinePackDelegate and made its methods required, since MGLOfflineStorage is the only conforming implementation. Require a pack to be explicitly invalidated before deallocation by its owner (MGLOfflineStorage). Added a method to reload the canonical collection of packs.

Previously, if the user adds an offline pack without specifying a name in the alert, the pack would be named “Untitled n” for the nth pack created since the application was launched. This logic could result in identically named packs (for example, if the user relaunches and creates another unnamed pack). Instead, use the visible coordinate bounds as a placeholder and fallback name.

Resume a pack upon successful addition. Removed an extraneous weakSelf/strongSelf dance. When possible, update individual labels in a cell instead of reloading the entire cell. That way, if the user swipes to the left to reveal the Delete accessory button while the pack is actively downloading, the accessory button remains on screen as progress updates come in.
Removed NS_UNAVAILABLE attribute from -[MGLOfflinePack init]. Packs initialized with this initializer are doomed to invalidity.

Fixes #4421.
Updated MGLTilePyramidOfflineRegion documentation to note that local files can’t be used as style URLs. Added documentation for private offline methods.
Fixed an incomplete variable renaming. Don’t bother creating intermediate directories to the legacy cache path. When the main bundle lacks a bundle identifier, as is the case when the SDK runs in the test bundle, fall back on the SDK’s bundle.
Implemented NSSecureCoding and NSCopying protocols for MGLTilePyramidOfflineRegion so that it can be used more fluidly by client code.
Added unit tests for MGLTilePyramidOfflineRegion, MGLOfflinePack, and MGLOfflineStorage.

Fixes #4426.
An MGLOfflinePackProgressChangedNotification notification now includes the pack’s state and progress values, which match the values in the corresponding MGLOfflinePack properties. Implemented NSValue category methods to store MGLOfflinePackProgress inside a dictionary.
Added an offline pack download manager to osxapp to demonstrate the use of MGLOfflineStorage with Cocoa Bindings. Like the Offline Packs view controller in iosapp, new packs are added based on the foremost map view’s current state. Double-clicking a complete offline pack opens a new map window to the pack’s bounds.
Requesting progress is expensive, so it is once again computed only on demand, rather than automatically at launch. Moved progress requesting back to iosapp and osxapp.
The previous change includes an additional salient change: a newly minted offline pack’s state is inactive initially, not unknown.
@1ec5
1ec5 force-pushed the 1ec5-offline-kvo-4287 branch from 0c197fa to 9a556bf Compare March 29, 2016 18:46
@1ec5
1ec5 merged commit 9a556bf into release-ios-3.2.0-android-4.0.0 Mar 29, 2016
@1ec5
1ec5 deleted the 1ec5-offline-kvo-4287 branch March 29, 2016 18:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

crash iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS offline refactor tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants