diff options
author | Julian Rex <julian.rex@mapbox.com> | 2018-11-15 15:32:03 -0500 |
---|---|---|
committer | Julian Rex <julian.rex@mapbox.com> | 2018-11-20 10:15:59 -0500 |
commit | 8f4a4f83b9cd7415d6b371642e72bc99e994bda0 (patch) | |
tree | a91a3d3480e3f0125a6a8c82b5c91435f447703c | |
parent | dbb2b2e606f85f6074f5bc54407045f0607a7f92 (diff) | |
download | qtlocation-mapboxgl-8f4a4f83b9cd7415d6b371642e72bc99e994bda0.tar.gz |
Added code in MGLMapSnapshotter to observe app termination
-rw-r--r-- | platform/darwin/src/MGLMapSnapshotter.h | 4 | ||||
-rw-r--r-- | platform/darwin/src/MGLMapSnapshotter.mm | 45 |
2 files changed, 47 insertions, 2 deletions
diff --git a/platform/darwin/src/MGLMapSnapshotter.h b/platform/darwin/src/MGLMapSnapshotter.h index de509f73f4..2bd9d99fe0 100644 --- a/platform/darwin/src/MGLMapSnapshotter.h +++ b/platform/darwin/src/MGLMapSnapshotter.h @@ -173,6 +173,8 @@ typedef void (^MGLMapSnapshotCompletionHandler)(MGLMapSnapshot* _Nullable snapsh MGL_EXPORT @interface MGLMapSnapshotter : NSObject +- (instancetype)init NS_UNAVAILABLE; + /** Initializes and returns a map snapshotter object that produces snapshots according to the given options. @@ -180,7 +182,7 @@ MGL_EXPORT @param options The options to use when generating a map snapshot. @return An initialized map snapshotter. */ -- (instancetype)initWithOptions:(MGLMapSnapshotOptions *)options; +- (instancetype)initWithOptions:(MGLMapSnapshotOptions *)options NS_DESIGNATED_INITIALIZER; /** Starts the snapshot creation and executes the specified block with the result. diff --git a/platform/darwin/src/MGLMapSnapshotter.mm b/platform/darwin/src/MGLMapSnapshotter.mm index 3c5e36f8f2..4ecdeef0e9 100644 --- a/platform/darwin/src/MGLMapSnapshotter.mm +++ b/platform/darwin/src/MGLMapSnapshotter.mm @@ -117,6 +117,7 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; @interface MGLMapSnapshotter() @property (nonatomic) BOOL cancelled; +@property (nonatomic) BOOL terminated; @property (nonatomic) dispatch_queue_t resultQueue; @property (nonatomic, copy) MGLMapSnapshotCompletionHandler completion; + (void)completeWithErrorCode:(MGLErrorCode)errorCode description:(nonnull NSString*)description onQueue:(dispatch_queue_t)queue completion:(MGLMapSnapshotCompletionHandler)completion; @@ -129,6 +130,8 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; } - (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; + if (_completion) { MGLAssert(_snapshotCallback, @"Snapshot in progress - there should be a valid callback"); @@ -139,16 +142,40 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; } } + +- (instancetype)init { + NSAssert(NO, @"Please use -[MGLMapSnapshotter initWithOptions:]"); + [super doesNotRecognizeSelector:_cmd]; + return nil; +} + - (instancetype)initWithOptions:(MGLMapSnapshotOptions *)options { MGLLogDebug(@"Initializing withOptions: %@", options); self = [super init]; if (self) { [self setOptions:options]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(applicationWillTerminate:) + name:UIApplicationWillTerminateNotification + object:nil]; } return self; } +- (void)applicationWillTerminate:(NSNotification *)notification +{ + if (self.completion) { + [self cancel]; + } + + _mbglMapSnapshotter.reset(); + _snapshotCallback.reset(); + _mbglThreadPool.reset(); + + self.terminated = YES; +} + - (void)startWithCompletionHandler:(MGLMapSnapshotCompletionHandler)completion { MGLLogDebug(@"Starting withCompletionHandler: %@", completion); @@ -167,7 +194,12 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; [NSException raise:NSInternalInconsistencyException format:@"Already started this snapshotter."]; } - + + if (self.terminated) { + [NSException raise:NSInternalInconsistencyException + format:@"Starting a snapshotter after application termination is not supported."]; + } + self.completion = completion; self.resultQueue = queue; self.cancelled = NO; @@ -539,7 +571,18 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; - (void)setOptions:(MGLMapSnapshotOptions *)options { + if (_terminated) { + [NSException raise:NSInternalInconsistencyException + format:@"Calling MGLMapSnapshotter.options after application termination is not supported."]; + } + MGLLogDebug(@"Setting options: %@", options); + + if (_completion) { + [self cancel]; + } + + _cancelled = NO; _options = options; mbgl::DefaultFileSource *mbglFileSource = [MGLOfflineStorage sharedOfflineStorage].mbglFileSource; _mbglThreadPool = mbgl::sharedThreadPool(); |