From 45892e822bf5af0af8ce2f65dd0fa182219b6e4b Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 11 Apr 2018 15:29:50 -0400 Subject: [PATCH] Move 'background' check in message processing logic off main thread. --- .../src/Messages/OWSBatchMessageProcessor.m | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m b/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m index 46edb23d7..772cb5ca6 100644 --- a/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m +++ b/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m @@ -225,6 +225,7 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo @property (nonatomic, readonly) YapDatabaseConnection *dbConnection; @property (nonatomic, readonly) OWSMessageContentJobFinder *finder; @property (nonatomic) BOOL isDrainingQueue; +@property (atomic) BOOL isAppInBackground; - (instancetype)initWithMessagesManager:(OWSMessageManager *)messagesManager primaryStorage:(OWSPrimaryStorage *)primaryStorage @@ -253,6 +254,15 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo _finder = finder; _isDrainingQueue = NO; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(applicationWillEnterForeground:) + name:OWSApplicationWillEnterForegroundNotification + object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(applicationDidEnterBackground:) + name:OWSApplicationDidEnterBackgroundNotification + object:nil]; + // Start processing. [AppReadiness runNowOrWhenAppIsReady:^{ [self drainQueue]; @@ -261,6 +271,23 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo return self; } +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + +#pragma mark - Notifications + +- (void)applicationWillEnterForeground:(NSNotification *)notification +{ + self.isAppInBackground = NO; +} + +- (void)applicationDidEnterBackground:(NSNotification *)notification +{ + self.isAppInBackground = YES; +} + #pragma mark - instance methods - (dispatch_queue_t)serialQueue @@ -353,7 +380,7 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo transaction:transaction]; [processedJobs addObject:job]; - if (CurrentAppContext().isInBackground) { + if (self.isAppInBackground) { // If the app is in the background, stop processing this batch. // // Since this check is done after processing jobs, we'll continue