[ASMapNode] Reduce number of MapKit calls to -cancel by using the .isLoading property.

This commit is contained in:
Scott Goodson 2016-03-18 23:30:26 -07:00
parent 0745eabec9
commit 7bb48d266c

View File

@ -134,14 +134,16 @@
- (void)setOptions:(MKMapSnapshotOptions *)options - (void)setOptions:(MKMapSnapshotOptions *)options
{ {
ASDN::MutexLocker l(_propertyLock); ASDN::MutexLocker l(_propertyLock);
if (!_options || ![options isEqual:_options]) {
_options = options; _options = options;
if (self.isLiveMap) { if (self.isLiveMap) {
[self applySnapshotOptions]; [self applySnapshotOptions];
} else { } else if (_snapshotter) {
[self resetSnapshotter]; [self destroySnapshotter];
[self takeSnapshot]; [self takeSnapshot];
} }
} }
}
- (MKCoordinateRegion)region - (MKCoordinateRegion)region
{ {
@ -160,7 +162,11 @@
if (!_snapshotter) { if (!_snapshotter) {
[self setUpSnapshotter]; [self setUpSnapshotter];
} }
[_snapshotter cancel];
if (_snapshotter.isLoading) {
return;
}
[_snapshotter startWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) [_snapshotter startWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
completionHandler:^(MKMapSnapshot *snapshot, NSError *error) { completionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
if (!error) { if (!error) {
@ -207,12 +213,10 @@
_snapshotter = [[MKMapSnapshotter alloc] initWithOptions:self.options]; _snapshotter = [[MKMapSnapshotter alloc] initWithOptions:self.options];
} }
- (void)resetSnapshotter - (void)destroySnapshotter
{ {
// FIXME: The semantics of this method / name would suggest that we cancel + destroy the snapshotter,
// but not that we create a new one. We should probably only create the new one in -takeSnapshot or something.
[_snapshotter cancel]; [_snapshotter cancel];
_snapshotter = [[MKMapSnapshotter alloc] initWithOptions:self.options]; _snapshotter = nil;
} }
- (void)applySnapshotOptions - (void)applySnapshotOptions
@ -273,11 +277,14 @@
} }
#pragma mark - Layout #pragma mark - Layout
- (void)setSnapshotSizeIfNeeded:(CGSize)snapshotSize - (void)setSnapshotSizeWithReloadIfNeeded:(CGSize)snapshotSize
{ {
if (!CGSizeEqualToSize(self.options.size, snapshotSize)) { if (!CGSizeEqualToSize(self.options.size, snapshotSize)) {
_options.size = snapshotSize; _options.size = snapshotSize;
[self resetSnapshotter]; if (_snapshotter) {
[self destroySnapshotter];
[self takeSnapshot];
}
} }
} }
@ -287,11 +294,11 @@
if (CGSizeEqualToSize(size, CGSizeZero)) { if (CGSizeEqualToSize(size, CGSizeZero)) {
size = constrainedSize; size = constrainedSize;
} }
[self setSnapshotSizeIfNeeded:size]; [self setSnapshotSizeWithReloadIfNeeded:size];
return constrainedSize; return constrainedSize;
} }
// Layout isn't usually needed in the box model, but since we are making use of MKMapView this is preferred. // -layout isn't usually needed over -layoutSpecThatFits, but this way we can avoid a needless node wrapper for MKMapView.
- (void)layout - (void)layout
{ {
[super layout]; [super layout];
@ -300,10 +307,9 @@
} else { } else {
// If our bounds.size is different from our current snapshot size, then let's request a new image from MKMapSnapshotter. // If our bounds.size is different from our current snapshot size, then let's request a new image from MKMapSnapshotter.
if (_needsMapReloadOnBoundsChange) { if (_needsMapReloadOnBoundsChange) {
[self setSnapshotSizeIfNeeded:self.bounds.size]; [self setSnapshotSizeWithReloadIfNeeded:self.bounds.size];
// FIXME: Adding a check for FetchData here seems to cause intermittent map load failures, but shouldn't. // FIXME: Adding a check for FetchData here seems to cause intermittent map load failures, but shouldn't.
// if (ASInterfaceStateIncludesFetchData(self.interfaceState)) { // if (ASInterfaceStateIncludesFetchData(self.interfaceState)) {
[self takeSnapshot];
} }
} }
} }