From 72086edb6e95905bd85ab1e0b485fec16379735b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Wed, 20 Apr 2016 08:43:05 -0700 Subject: [PATCH] [ios, osx] Avoid observing coordinate on MGLMultiPoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There’s no point observing the coordinate key path on MGLMultiPoint, since that property is immutable. Use the observation context instead of performing an annotation tag lookup when a coordinate has changed. --- platform/ios/CHANGELOG.md | 2 +- platform/ios/src/MGLMapView.mm | 26 ++++++++++---------------- platform/osx/src/MGLMapView.mm | 27 ++++++++++++--------------- 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/platform/ios/CHANGELOG.md b/platform/ios/CHANGELOG.md index 81a91395a..faee9409c 100644 --- a/platform/ios/CHANGELOG.md +++ b/platform/ios/CHANGELOG.md @@ -8,7 +8,7 @@ Mapbox welcomes participation and contributions from everyone. If you’d like - Removed the `armv7s` slice from the SDK to reduce its size. iPhone 5 and iPhone 5c automatically use the `armv7` slice instead. ([#4641](https://github.com/mapbox/mapbox-gl-native/pull/4641)) - The user dot now moves smoothly between user location updates while user location tracking is disabled. ([#1582](https://github.com/mapbox/mapbox-gl-native/pull/1582)) - User location heading updates now resume properly when an app becomes active again. ([#4674](https://github.com/mapbox/mapbox-gl-native/pull/4674)) -- An MGLAnnotation can be relocated by changing its `coordinate` property in a KVO-compliant way. ([#3835](https://github.com/mapbox/mapbox-gl-native/pull/3835)) +- An MGLAnnotation can be relocated by changing its `coordinate` property in a KVO-compliant way. An MGLMultiPoint cannot be relocated. ([#3835](https://github.com/mapbox/mapbox-gl-native/pull/3835)) - Setting the `image` property of an MGLAnnotationImage to `nil` resets it to the default red pin image and reclaims resources that can be used to customize additional annotations. ([#3835](https://github.com/mapbox/mapbox-gl-native/pull/3835)) - Fixed an issue preventing KVO change notifications from being generated on MGLMapView’s `userTrackingMode` key path when `-setUserTrackingMode:animated:` is called. ([#4724](https://github.com/mapbox/mapbox-gl-native/pull/4724)) - Fixed a hang that could occur if the host application attempts to set user defaults on a background queue. ([#4745](https://github.com/mapbox/mapbox-gl-native/pull/4745)) diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 507a0846d..1b07c68cf 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -1597,7 +1597,7 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) #pragma mark - Properties - -- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(__unused void *)context +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"hidden"] && object == _attributionButton) { @@ -1608,19 +1608,16 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) [MGLMapboxEvents ensureMetricsOptoutExists]; } } - else if ([keyPath isEqualToString:@"coordinate"] && [object conformsToProtocol:@protocol(MGLAnnotation)]) + else if ([keyPath isEqualToString:@"coordinate"] && [object conformsToProtocol:@protocol(MGLAnnotation)] && ![object isKindOfClass:[MGLMultiPoint class]]) { id annotation = object; - MGLAnnotationTag annotationTag = [self annotationTagForAnnotation:annotation]; - if (annotationTag != MGLAnnotationTagNotFound) + MGLAnnotationTag annotationTag = (MGLAnnotationTag)(NSUInteger)context; + const mbgl::LatLng latLng = MGLLatLngFromLocationCoordinate2D(annotation.coordinate); + MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag]; + _mbglMap->updatePointAnnotation(annotationTag, { latLng, annotationImage.styleIconIdentifier.UTF8String ?: "" }); + if (annotationTag == _selectedAnnotationTag) { - const mbgl::LatLng latLng = MGLLatLngFromLocationCoordinate2D(annotation.coordinate); - MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag]; - _mbglMap->updatePointAnnotation(annotationTag, { latLng, annotationImage.styleIconIdentifier.UTF8String ?: "" }); - if (annotationTag == _selectedAnnotationTag) - { - [self deselectAnnotation:annotation animated:YES]; - } + [self deselectAnnotation:annotation animated:YES]; } } } @@ -2452,6 +2449,7 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) _annotationContextsByAnnotationTag[annotationTag] = context; if ([annotation isKindOfClass:[NSObject class]]) { + NSAssert(![annotation isKindOfClass:[MGLMultiPoint class]], @"Point annotation should not be MGLMultiPoint."); [(NSObject *)annotation addObserver:self forKeyPath:@"coordinate" options:0 context:(void *)(NSUInteger)annotationTag]; } } @@ -2469,10 +2467,6 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) MGLAnnotationContext context; context.annotation = annotation; _annotationContextsByAnnotationTag[annotationTag] = context; - - if ([annotation isKindOfClass:[NSObject class]]) { - [(NSObject *)annotation addObserver:self forKeyPath:@"coordinate" options:0 context:(void *)(NSUInteger)annotationTag]; - } } } @@ -2600,7 +2594,7 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) _annotationContextsByAnnotationTag.erase(annotationTag); - if ([annotation isKindOfClass:[NSObject class]]) + if ([annotation isKindOfClass:[NSObject class]] && ![annotation isKindOfClass:[MGLMultiPoint class]]) { [(NSObject *)annotation removeObserver:self forKeyPath:@"coordinate" context:(void *)(NSUInteger)annotationTag]; } diff --git a/platform/osx/src/MGLMapView.mm b/platform/osx/src/MGLMapView.mm index 8be4772c3..69bc5c87b 100644 --- a/platform/osx/src/MGLMapView.mm +++ b/platform/osx/src/MGLMapView.mm @@ -460,21 +460,20 @@ public: } } -- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(__unused id)object change:(__unused NSDictionary *)change context:(__unused void *)context { +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(__unused NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"contentLayoutRect"] || [keyPath isEqualToString:@"titlebarAppearsTransparent"]) { [self adjustContentInsets]; } else if ([keyPath isEqualToString:@"coordinate"] && - [object conformsToProtocol:@protocol(MGLAnnotation)]) { + [object conformsToProtocol:@protocol(MGLAnnotation)] && + ![object isKindOfClass:[MGLMultiPoint class]]) { id annotation = object; - MGLAnnotationTag annotationTag = [self annotationTagForAnnotation:annotation]; - if (annotationTag != MGLAnnotationTagNotFound) { - const mbgl::LatLng latLng = MGLLatLngFromLocationCoordinate2D(annotation.coordinate); - MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag]; - _mbglMap->updatePointAnnotation(annotationTag, { latLng, annotationImage.styleIconIdentifier.UTF8String ?: "" }); - if (annotationTag == _selectedAnnotationTag) { - [self deselectAnnotation:annotation]; - } + MGLAnnotationTag annotationTag = (MGLAnnotationTag)(NSUInteger)context; + const mbgl::LatLng latLng = MGLLatLngFromLocationCoordinate2D(annotation.coordinate); + MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag]; + _mbglMap->updatePointAnnotation(annotationTag, { latLng, annotationImage.styleIconIdentifier.UTF8String ?: "" }); + if (annotationTag == _selectedAnnotationTag) { + [self deselectAnnotation:annotation]; } } } @@ -1608,6 +1607,7 @@ public: _annotationContextsByAnnotationTag[annotationTag] = context; if ([annotation isKindOfClass:[NSObject class]]) { + NSAssert(![annotation isKindOfClass:[MGLMultiPoint class]], @"Point annotation should not be MGLMultiPoint."); [(NSObject *)annotation addObserver:self forKeyPath:@"coordinate" options:0 context:(void *)(NSUInteger)annotationTag]; } } @@ -1624,10 +1624,6 @@ public: MGLAnnotationContext context; context.annotation = annotation; _annotationContextsByAnnotationTag[annotationTag] = context; - - if ([annotation isKindOfClass:[NSObject class]]) { - [(NSObject *)annotation addObserver:self forKeyPath:@"coordinate" options:0 context:(void *)(NSUInteger)annotationTag]; - } } } @@ -1720,7 +1716,8 @@ public: _annotationContextsByAnnotationTag.erase(annotationTag); - if ([annotation isKindOfClass:[NSObject class]]) { + if ([annotation isKindOfClass:[NSObject class]] && + ![annotation isKindOfClass:[MGLMultiPoint class]]) { [(NSObject *)annotation removeObserver:self forKeyPath:@"coordinate" context:(void *)(NSUInteger)annotationTag]; } }