Move 'background' check in message processing logic off main thread.

This commit is contained in:
Matthew Chen 2018-04-11 15:29:50 -04:00
parent 5f56269f9e
commit 45892e822b
1 changed files with 28 additions and 1 deletions

View File

@ -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