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

[ios] MGLAnnotationView annotation property made writable #8139

Merged
merged 1 commit into from
Mar 2, 2017

Conversation

fabian-guerra
Copy link
Contributor

Fixes #7321

@fabian-guerra fabian-guerra added annotations Annotations on iOS and macOS or markers on Android iOS Mapbox Maps SDK for iOS MapKit parity For feature parity with MapKit on iOS or macOS labels Feb 21, 2017
@fabian-guerra fabian-guerra added this to the ios-v3.5.0 milestone Feb 21, 2017
@fabian-guerra fabian-guerra self-assigned this Feb 21, 2017
@mention-bot
Copy link

@fabian-guerra, thanks for your PR! By analyzing this pull request, we identified @boundsj, @1ec5 and @incanus to be potential reviewers.

@@ -89,7 +89,7 @@ typedef NS_ENUM(NSUInteger, MGLAnnotationViewDragState) {
contains a non-`nil` value while the annotation view is visible on the map. If
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If setAnnotation exists publicly, then they can override it in a subclass and have the annotation model change the behavior of the view.

@@ -89,7 +89,7 @@ typedef NS_ENUM(NSUInteger, MGLAnnotationViewDragState) {
contains a non-`nil` value while the annotation view is visible on the map. If
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor Author

@fabian-guerra fabian-guerra Feb 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

@incanus incanus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-nil value only while the annotation view is visible on the map. If the view is queued and waiting to be reused, the value is nil

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.

@fabian-guerra
Copy link
Contributor Author

@incanus #8139 (comment) does this makes sense?

@incanus
Copy link
Contributor

incanus commented Feb 21, 2017

@fabian-guerra Yes, but I think we either need to guard against, or at very least warn against, setting the annotation to nil since it's technically now possible. Especially if that can result in a crash.

@fabian-guerra
Copy link
Contributor Author

@incanus I'm thinking a NSAssert clause would be better since it won't hide the failing reason nor creating unexpected behavior, thoughts?

@eimantas
Copy link
Contributor

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.

@fabian-guerra fabian-guerra force-pushed the fabian-read-only-annotation branch from 17eb636 to a74333f Compare February 22, 2017 15:47
@incanus
Copy link
Contributor

incanus commented Feb 28, 2017

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.

@fabian-guerra fabian-guerra force-pushed the fabian-read-only-annotation branch from 590c6ce to a74333f Compare March 2, 2017 00:19
@fabian-guerra fabian-guerra merged commit 8a5cc35 into master Mar 2, 2017
@fabian-guerra fabian-guerra deleted the fabian-read-only-annotation branch March 2, 2017 23:19
@boundsj
Copy link
Contributor

boundsj commented Mar 3, 2017

@fabian-guerra please cherry pick this to the new release branch release-ios-v3.5.0-android-v5.0.0 thanks!

@@ -240,6 +239,7 @@ - (void)setDragState:(MGLAnnotationViewDragState)dragState animated:(BOOL)animat
}
else if (dragState == MGLAnnotationViewDragStateCanceling)
{
NSAssert(self.annotation, @"Annotation property should not be nil.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@1ec5 1ec5 mentioned this pull request Mar 3, 2017
1ec5 added a commit that referenced this pull request Mar 10, 2017
Mentioned #8298, #8139. Copyedited recent additions.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
annotations Annotations on iOS and macOS or markers on Android iOS Mapbox Maps SDK for iOS MapKit parity For feature parity with MapKit on iOS or macOS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants