From 0027f5cdc2c266695ca86d441ab694366820b664 Mon Sep 17 00:00:00 2001 From: Jesse Bounds Date: Mon, 27 Feb 2017 12:03:10 -0800 Subject: [PATCH] [ios] Pause file source activity on background This uses the pause/resume API on the default file source to pause network and revalidation activity when the host iOS application goes into the background. Activity is resumed when the host application goes into the foreground. The intention of this change is to avoid edge cases on some OSs where resources (i.e. sqlite) are not available when the host app is not visible to the user. --- platform/darwin/src/MGLOfflineStorage.mm | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index 64f9111f1..9cb472ce5 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -40,12 +40,26 @@ NSString * const MGLOfflinePackMaximumCountUserInfoKey = MGLOfflinePackUserInfoK static MGLOfflineStorage *sharedOfflineStorage; dispatch_once(&onceToken, ^{ sharedOfflineStorage = [[self alloc] init]; +#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR + [[NSNotificationCenter defaultCenter] addObserver:sharedOfflineStorage selector:@selector(unpauseFileSource:) name:UIApplicationWillEnterForegroundNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:sharedOfflineStorage selector:@selector(pauseFileSource:) name:UIApplicationDidEnterBackgroundNotification object:nil]; +#endif [sharedOfflineStorage reloadPacks]; }); return sharedOfflineStorage; } +#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR +- (void)pauseFileSource:(__unused NSNotification *)notification { + _mbglFileSource->pause(); +} + +- (void)unpauseFileSource:(__unused NSNotification *)notification { + _mbglFileSource->resume(); +} +#endif + - (void)setDelegate:(id)newValue { _delegate = newValue; if ([self.delegate respondsToSelector:@selector(offlineStorage:URLForResourceOfKind:withURL:)]) { @@ -203,6 +217,7 @@ NSString * const MGLOfflinePackMaximumCountUserInfoKey = MGLOfflinePackUserInfoK } - (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; [[MGLNetworkConfiguration sharedManager] removeObserver:self forKeyPath:@"apiBaseURL"]; [[MGLAccountManager sharedManager] removeObserver:self forKeyPath:@"accessToken"];