-
Notifications
You must be signed in to change notification settings - Fork 122
Keep MGLMapSnapshotter alive through completion #211
Conversation
@@ -16,7 +16,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT | |||
|
|||
### Snapshots | |||
|
|||
* `MGLMapSnapshotter` no longer keeps itself from being deallocated before its completion handler is called. To ensure that the completion handler passed into `-[MGLMapSnapshotter startWithCompletionHandler:]` is called, maintain a strong reference to the snapshotter from a longer-lived class, such as the class where you call `-[MGLMapSnapshotter startWithCompletionHandler:]`. ([#210](https://github.com/mapbox/mapbox-gl-native-ios/pull/210)) | |||
* You no longer need to explicitly capture the `MGLMapSnapshotter` object in the completion handler that you specify in `-[MGLMapSnapshotter startWithCompletionHandler:]`. Even if you declare the snapshotter locally without holding a strong reference to it, the snapshotter is only deallocated after the completion handler finishes, and the completion handler generally receives a valid snapshot. ([#210](https://github.com/mapbox/mapbox-gl-native-ios/pull/210)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably should link to this PR instead of #210.
weakSnapshotter = snapshotter; | ||
|
||
[snapshotter startWithCompletionHandler:^(MGLMapSnapshot * _Nullable snapshot, NSError * _Nullable error) { | ||
// We expect this completion block to be called with an error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is inaccurate: it crept back in when I reverted #210’s changes to the test, but we want the completion handler to be called with a valid snapshot now.
272fdfe
to
944388e
Compare
platform/ios/Integration Tests/Snapshotter Tests/MGLMapSnapshotterTest.m
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm in favour of landing this PR with #210 - I feel it's solves a lot of issues.
Just a question about one of the tests - is this a comment/function name issue or just a typo?
055a482
to
d357f8c
Compare
54f4f6d
to
363610c
Compare
Strongly capture MGLMapSnapshotter until its completion handler finishes executing. The developer no longer needs to explicitly capture the snapshotter in the completion handler to prevent it from being canceled or deallocated, because the internal completion handler does so for the developer.
Before #210, a snapshotter that wasn’t captured in its completion handler would be deallocated as soon as it went out of scope, but the completion handler would be called with an error “headlessly” after the snapshotter got deallocated. As of #210, the same snapshotter would’ve silently gone away without calling the completion handler, since it was considered the responsibility of the developer to hang onto the snapshotter. As of this PR, the developer is no longer required to hang onto the snapshotter, and the completion handler will generally receive a snapshot rather than an error.
This new behavior matches MapKit’s MKMapSnapshotter and is ideal for one-off usage. I had shied away from it in #200 (comment) because of the complexity around keeping the completion handler alive, but #210 gives us more confidence about the control flow and lifetime of any captured state. Nonetheless, this PR needs extra scrutiny because it possibly reintroduces edge cases that #210 had eliminated by reducing the snapshotter’s scope. In particular, we should make sure the application can reliably terminate while snapshotters are active without crashing.
Depends on #210. Fixes mapbox/mapbox-gl-native#12336 in a different way.
/cc @mapbox/maps-ios @alexshalamov