Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Keep MGLMapSnapshotter alive through completion #211

Merged
merged 2 commits into from
Mar 17, 2020

Conversation

1ec5
Copy link
Contributor

@1ec5 1ec5 commented Mar 14, 2020

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

@@ -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))
Copy link
Contributor Author

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
Copy link
Contributor Author

@1ec5 1ec5 Mar 14, 2020

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.

@1ec5 1ec5 force-pushed the 1ec5-snapshotter-keep-alive-12336 branch from 272fdfe to 944388e Compare March 16, 2020 18:31
Copy link
Contributor

@julianrex julianrex left a 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?

@1ec5 1ec5 force-pushed the 1ec5-snapshotter-parallel-state-209 branch from 055a482 to d357f8c Compare March 17, 2020 01:15
@1ec5 1ec5 force-pushed the 1ec5-snapshotter-keep-alive-12336 branch from 54f4f6d to 363610c Compare March 17, 2020 01:17
@1ec5 1ec5 changed the base branch from 1ec5-snapshotter-parallel-state-209 to master March 17, 2020 02:38
@1ec5 1ec5 merged commit 363610c into master Mar 17, 2020
@1ec5 1ec5 deleted the 1ec5-snapshotter-keep-alive-12336 branch March 17, 2020 02:38
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request ios macOS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants