[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.
This commit is contained in:
@@ -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<MGLOfflineStorageDelegate>)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"];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user