diff options
author | Julian Rex <julian.rex@gmail.com> | 2018-11-20 14:31:09 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-20 14:31:09 -0500 |
commit | 53bbf9d6991c1eddfa141ddcfb4b7252874e46ec (patch) | |
tree | 3ad8c97d579c4086b3682e27e6ff3deae52680f6 /platform/darwin | |
parent | 8b232f693dc3f835bbb91c6c4bdf07020e98c164 (diff) | |
download | qtlocation-mapboxgl-53bbf9d6991c1eddfa141ddcfb4b7252874e46ec.tar.gz |
[ios] Added teardown of core objects when receiving UIApplicationWillTerminateNotification (#13367)
Diffstat (limited to 'platform/darwin')
-rw-r--r-- | platform/darwin/src/MGLMapSnapshotter.h | 4 | ||||
-rw-r--r-- | platform/darwin/src/MGLMapSnapshotter.mm | 46 |
2 files changed, 48 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..639a3b62ee 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,41 @@ 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]; +#if TARGET_OS_IPHONE + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil]; +#else + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil]; +#endif } 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 +195,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 +572,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(); |