From 92276157dca1edcefeabeaac933aacaa0330be27 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 6 Jul 2017 12:34:08 -0400 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20sync=20verification=20state=20u?= =?UTF-8?q?ntil=20app=20has=20finished=20becoming=20active.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit // FREEBIE --- src/Messages/OWSIdentityManager.m | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Messages/OWSIdentityManager.m b/src/Messages/OWSIdentityManager.m index a518def95..ca0e87076 100644 --- a/src/Messages/OWSIdentityManager.m +++ b/src/Messages/OWSIdentityManager.m @@ -93,12 +93,8 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa OWSSingletonAssert(); - // We want to observe these notifications lazily to avoid accessing - // the data store in [application: didFinishLaunchingWithOptions:]. - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)1.f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ - [self tryToSyncQueuedVerificationStates]; - [self observeNotifications]; - }); + [self observeNotifications]; + return self; } @@ -443,12 +439,23 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa inCollection:OWSIdentityManager_QueuedVerificationStateSyncMessages]; } - [self tryToSyncQueuedVerificationStates]; + dispatch_async(dispatch_get_main_queue(), ^{ + [self tryToSyncQueuedVerificationStates]; + }); }); } - (void)tryToSyncQueuedVerificationStates { + OWSAssert([NSThread isMainThread]); + + if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) { + // Only try to sync if the app is active to avoid interfering with startup. + // + // applicationDidBecomeActive: will try to sync again when the app becomes active. + return; + } + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ @synchronized(self) { @@ -753,7 +760,11 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa { OWSAssert([NSThread isMainThread]); - [self tryToSyncQueuedVerificationStates]; + // We want to defer this so that we never call this method until + // [UIApplicationDelegate applicationDidBecomeActive:] is complete. + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)1.f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ + [self tryToSyncQueuedVerificationStates]; + }); } #pragma mark - Logging