[ios] MGLAnnotationView annotation property made writable#8139
Conversation
|
@fabian-guerra, thanks for your PR! By analyzing this pull request, we identified @boundsj, @1ec5 and @incanus to be potential reviewers. |
| the view is queued, waiting to be reused, the value is `nil`. | ||
| */ | ||
| @property (nonatomic, readonly, nullable) id <MGLAnnotation> annotation; | ||
| @property (nonatomic, nullable) id <MGLAnnotation> annotation; |
There was a problem hiding this comment.
What is the effect of changing this property? Does it accomplish the use case envisioned in #7321, given that MGLAnnotationView’s initializer doesn’t take an MGLAnnotation parameter?
There was a problem hiding this comment.
If setAnnotation exists publicly, then they can override it in a subclass and have the annotation model change the behavior of the view.
| the view is queued, waiting to be reused, the value is `nil`. | ||
| */ | ||
| @property (nonatomic, readonly, nullable) id <MGLAnnotation> annotation; | ||
| @property (nonatomic, nullable) id <MGLAnnotation> annotation; |
There was a problem hiding this comment.
Even though MKAnnotationView.annotation is read-write, it also comes with this note in the documentation:
You should not change the value of this property directly.
which we also state above, redundantly. What is our reason for making the property read-write but advising developers not to write to it?
There was a problem hiding this comment.
Changing the annotation in mapView:viewForAnnotation: will not affect annotationViewForAnnotation: (the caller) it's meant to be a convenient way to set up any UI update code. This is also how Mapkit behaves.
From a conversation with @boundsj
The developer setting it in the delegate first is redundant but facilitates them writing code to update the view when the annotation model property is set.
incanus
left a comment
There was a problem hiding this comment.
If setting an annotation view's annotation to nil is opened up to the public, since it's still nullable, that means that it could go away at any time. If that's the case, it means instances such as this and this could crash.
Beyond fixing just these instances, we should come up good tests around various points in the lifecycle to ensure that setting an annotation to nil is safe.
Note also that as @eimantas points out in the OP in #7321 (comment), MapKit provides an initializer argument, but also has a note on the property itself:
You should not change the value of this property directly. This property contains a non-
nilvalue only while the annotation view is visible on the map. If the view is queued and waiting to be reused, the value isnil
We should determine if MapKit has any guards around this (for example, gating the public setter and only using a private one internally for its own management) and implement that.
|
@incanus #8139 (comment) does this makes sense? |
|
@fabian-guerra Yes, but I think we either need to guard against, or at very least warn against, setting the annotation to |
|
@incanus I'm thinking a |
|
One more approach would be to create a designated initialiser that accepts the annotation as an argument (just like in MapKit). This would also increase the MapKit parity for the mapbox' darwin SDKs. With this it'd be possible to mark the current initialiser as deprecated and leave it like that for a couple of releases until people who update the SDK, would know to update the initialisers too. |
17eb636 to
a74333f
Compare
|
Made a minor grammar tweak in 590c6ce @fabian-guerra, but otherwise this feels ok to me. I'm unsure how sustainable it will be in the future, but I think that just comes with the territory of this sort of change. |
590c6ce to
a74333f
Compare
|
@fabian-guerra please cherry pick this to the new release branch |
| } | ||
| else if (dragState == MGLAnnotationViewDragStateCanceling) | ||
| { | ||
| NSAssert(self.annotation, @"Annotation property should not be nil."); |
There was a problem hiding this comment.
When you want to indicate an error on the developer’s part, raise a named NSException instead of calling NSAssert(). See #8026 (comment) for more details. @fabian-guerra, can you change these assertions into named exceptions?
Fixes #7321