Respond to CR.

pull/1/head
Matthew Chen 7 years ago
parent 8930110efd
commit b7958262ba

@ -265,33 +265,12 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo
_finder = finder; _finder = finder;
_isDrainingQueue = NO; _isDrainingQueue = NO;
[[NSNotificationCenter defaultCenter] addObserver:self // Start processing.
selector:@selector(appIsReady) [AppReadiness runNowOrWhenAppIsReady:^{
name:AppIsReadyNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(yapDatabaseModified:)
name:YapDatabaseModifiedNotification
object:TSStorageManager.sharedManager.dbNotificationObject];
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)appIsReady
{
[self drainQueue]; [self drainQueue];
} }];
- (void)yapDatabaseModified:(NSNotification *)notification
{
OWSAssertIsOnMainThread();
[self drainQueue]; return self;
} }
#pragma mark - instance methods #pragma mark - instance methods
@ -319,24 +298,14 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo
- (void)drainQueue - (void)drainQueue
{ {
OWSAssert(AppReadiness.isAppReady);
// Don't process incoming messages in app extensions. // Don't process incoming messages in app extensions.
if (!CurrentAppContext().isMainApp) { if (!CurrentAppContext().isMainApp) {
return; return;
} }
dispatch_async(self.serialQueue, ^{ dispatch_async(self.serialQueue, ^{
if (!AppReadiness.isAppReady) {
// We don't want to process incoming messages until storage is ready.
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[AppReadiness runNowOrWhenAppIsReady:^{
[self drainQueue];
}];
});
return;
}
if (self.isDrainingQueue) { if (self.isDrainingQueue) {
return; return;
} }
@ -489,8 +458,13 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo
// We need to persist the decrypted envelope data ASAP to prevent data loss. // We need to persist the decrypted envelope data ASAP to prevent data loss.
[self.processingQueue enqueueEnvelopeData:envelopeData plaintextData:plaintextData transaction:transaction]; [self.processingQueue enqueueEnvelopeData:envelopeData plaintextData:plaintextData transaction:transaction];
// The new envelope won't be visible to the finder until this transaction commits, // The new envelope won't be visible to the finder until this transaction commits,
// so don't bother calling drainQueue here. // so drainQueue in the transaction completion.
[transaction addCompletionQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
completionBlock:^{
[self.processingQueue drainQueue];
}];
} }
@end @end

@ -243,24 +243,13 @@ NSString *const OWSMessageDecryptJobFinderExtensionGroup = @"OWSMessageProcessin
_finder = finder; _finder = finder;
_isDrainingQueue = NO; _isDrainingQueue = NO;
[[NSNotificationCenter defaultCenter] addObserver:self [AppReadiness runNowOrWhenAppIsReady:^{
selector:@selector(appIsReady) [self drainQueue];
name:AppIsReadyNotification }];
object:nil];
return self; return self;
} }
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)appIsReady
{
[self drainQueue];
}
#pragma mark - instance methods #pragma mark - instance methods
- (dispatch_queue_t)serialQueue - (dispatch_queue_t)serialQueue
@ -280,23 +269,14 @@ NSString *const OWSMessageDecryptJobFinderExtensionGroup = @"OWSMessageProcessin
- (void)drainQueue - (void)drainQueue
{ {
OWSAssert(AppReadiness.isAppReady);
// Don't decrypt messages in app extensions. // Don't decrypt messages in app extensions.
if (!CurrentAppContext().isMainApp) { if (!CurrentAppContext().isMainApp) {
return; return;
} }
dispatch_async(self.serialQueue, ^{ dispatch_async(self.serialQueue, ^{
if (!AppReadiness.isAppReady) {
// We don't want to process incoming messages until storage is ready.
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[AppReadiness runNowOrWhenAppIsReady:^{
[self drainQueue];
}];
});
return;
}
if (self.isDrainingQueue) { if (self.isDrainingQueue) {
return; return;
} }

@ -177,13 +177,10 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
OWSSingletonAssert(); OWSSingletonAssert();
[[NSNotificationCenter defaultCenter] addObserver:self // Start processing.
selector:@selector(appIsReady) [AppReadiness runNowOrWhenAppIsReady:^{
name:AppIsReadyNotification
object:nil];
// Try to start processing.
[self scheduleProcessing]; [self scheduleProcessing];
}];
return self; return self;
} }
@ -193,27 +190,14 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
[[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] removeObserver:self];
} }
- (void)appIsReady
{
[self scheduleProcessing];
}
// Schedules a processing pass, unless one is already scheduled. // Schedules a processing pass, unless one is already scheduled.
- (void)scheduleProcessing - (void)scheduleProcessing
{ {
OWSAssert(AppReadiness.isAppReady);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@synchronized(self) @synchronized(self)
{ {
if (!AppReadiness.isAppReady) {
DDLogInfo(@"%@ Deferring read receipt processing; storage not yet ready.", self.logTag);
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[AppReadiness runNowOrWhenAppIsReady:^{
[self scheduleProcessing];
}];
});
return;
}
if (self.isProcessing) { if (self.isProcessing) {
return; return;
} }

@ -4,8 +4,6 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
extern NSString *const AppIsReadyNotification;
typedef void (^AppReadyBlock)(void); typedef void (^AppReadyBlock)(void);
@interface AppReadiness : NSObject @interface AppReadiness : NSObject

@ -3,12 +3,9 @@
// //
#import "AppReadiness.h" #import "AppReadiness.h"
#import "NSNotificationCenter+OWS.h"
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
NSString *const AppIsReadyNotification = @"AppIsReadyNotification";
@interface AppReadiness () @interface AppReadiness ()
@property (atomic) BOOL isAppReady; @property (atomic) BOOL isAppReady;
@ -85,8 +82,6 @@ NSString *const AppIsReadyNotification = @"AppIsReadyNotification";
self.isAppReady = YES; self.isAppReady = YES;
[self runAppReadyBlocks]; [self runAppReadyBlocks];
[[NSNotificationCenter defaultCenter] postNotificationNameAsync:AppIsReadyNotification object:nil userInfo:nil];
} }
- (void)runAppReadyBlocks - (void)runAppReadyBlocks
@ -94,12 +89,10 @@ NSString *const AppIsReadyNotification = @"AppIsReadyNotification";
OWSAssertIsOnMainThread(); OWSAssertIsOnMainThread();
OWSAssert(self.isAppReady); OWSAssert(self.isAppReady);
// Make a local copy, then clear this state. for (AppReadyBlock block in self.appReadyBlocks) {
NSArray<AppReadyBlock> *appReadyBlocks = self.appReadyBlocks;
self.appReadyBlocks = nil;
for (AppReadyBlock block in appReadyBlocks) {
block(); block();
} }
self.appReadyBlocks = nil;
} }
@end @end

Loading…
Cancel
Save