Repository navigation
Centralize offline pack management with KVO compliance and tests - #4482
Conversation
|
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… |
0c43a3b to
9facc3f
Compare
|
Under what circumstances does this implementation call |
Yes, via |
|
😬 That makes listing an O(n^2) operation. |
|
In chat, @jfirebaugh suggested that we could obviate the use of 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. |
|
After reviewing the code, I don't see a way to include the status with
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 |
|
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 Based on these findings, I think we should at the very least defer calling |
|
Alright, making |
|
The hang on launch, incidentally, looks like this in iosapp (but doesn’t occur in osxapp): |
|
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. |
4368f37 to
552c78c
Compare
|
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. |
|
👍 |
2906cce to
0c197fa
Compare
|
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.
…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.
0c197fa to
9a556bf
Compare
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-requestProgresson 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-reloadPacksmethod 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_UNAVAILABLEattribute 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:
Fixes #4287, fixes #4394, fixes #4421, and fixes #4426.
/cc @jfirebaugh @boundsj @friedbunny @bsudekum