From 97169f521afaeee6472d9dfa70a0be1fb1087e4b Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 5 Jul 2017 17:54:22 -0400 Subject: [PATCH] =?UTF-8?q?Move=20more=20initialization=20until=20after=20?= =?UTF-8?q?didBecomeActive=20is=20complete=20to=20avoid=20the=20=E2=80=9Cb?= =?UTF-8?q?ad=20food=E2=80=9D=20crash.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit // FREEBIE --- Signal/src/AppDelegate.m | 94 +++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 44 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 0c45b54df..e3d2c3c87 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -136,50 +136,6 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; [self prepareScreenProtection]; - // At this point, potentially lengthy DB locking migrations could be running. - // Avoid blocking app launch by putting all further possible DB access in async thread. - [[TSAccountManager sharedInstance] - ifRegistered:YES - runAsync:^{ - DDLogInfo( - @"%@ running post launch block for registered user: %@", self.tag, [TSAccountManager localNumber]); - - [TSSocketManager requestSocketOpen]; - - RTCInitializeSSL(); - - [OWSSyncPushTokensJob runWithPushManager:[PushManager sharedManager] - accountManager:[Environment getCurrent].accountManager - preferences:[Environment preferences] - showAlerts:NO]; - - // Clean up any messages that expired since last launch immediately - // and continue cleaning in the background. - [[OWSDisappearingMessagesJob sharedJob] startIfNecessary]; - - // Mark all "attempting out" messages as "unsent", i.e. any messages that were not successfully - // sent before the app exited should be marked as failures. - [[[OWSFailedMessagesJob alloc] initWithStorageManager:[TSStorageManager sharedManager]] run]; - [[[OWSFailedAttachmentDownloadsJob alloc] initWithStorageManager:[TSStorageManager sharedManager]] run]; - - [AppStoreRating setupRatingLibrary]; - }]; - - [[TSAccountManager sharedInstance] - ifRegistered:NO - runAsync:^{ - dispatch_async(dispatch_get_main_queue(), ^{ - DDLogInfo(@"%@ running post launch block for unregistered user.", self.tag); - [TSSocketManager requestSocketOpen]; - - UITapGestureRecognizer *gesture = - [[UITapGestureRecognizer alloc] initWithTarget:[Pastelog class] action:@selector(submitLogs)]; - gesture.numberOfTapsRequired = 8; - [self.window addGestureRecognizer:gesture]; - }); - RTCInitializeSSL(); - }]; - self.contactsSyncing = [[OWSContactsSyncing alloc] initWithContactsManager:[Environment getCurrent].contactsManager identityManager:[OWSIdentityManager sharedManager] messageSender:[Environment getCurrent].messageSender]; @@ -505,6 +461,56 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; // Always check prekeys after app launches, and sometimes check on app activation. [TSPreKeyManager checkPreKeysIfNecessary]; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + // At this point, potentially lengthy DB locking migrations could be running. + // Avoid blocking app launch by putting all further possible DB access in async thread. + [[TSAccountManager sharedInstance] + ifRegistered:YES + runAsync:^{ + DDLogInfo(@"%@ running post launch block for registered user: %@", + self.tag, + [TSAccountManager localNumber]); + + [TSSocketManager requestSocketOpen]; + + RTCInitializeSSL(); + + [OWSSyncPushTokensJob runWithPushManager:[PushManager sharedManager] + accountManager:[Environment getCurrent].accountManager + preferences:[Environment preferences] + showAlerts:NO]; + + // Clean up any messages that expired since last launch immediately + // and continue cleaning in the background. + [[OWSDisappearingMessagesJob sharedJob] startIfNecessary]; + + // Mark all "attempting out" messages as "unsent", i.e. any messages that were not successfully + // sent before the app exited should be marked as failures. + [[[OWSFailedMessagesJob alloc] initWithStorageManager:[TSStorageManager sharedManager]] run]; + [[[OWSFailedAttachmentDownloadsJob alloc] initWithStorageManager:[TSStorageManager sharedManager]] + run]; + + [AppStoreRating setupRatingLibrary]; + }]; + + [[TSAccountManager sharedInstance] + ifRegistered:NO + runAsync:^{ + dispatch_async(dispatch_get_main_queue(), ^{ + DDLogInfo(@"%@ running post launch block for unregistered user.", self.tag); + [TSSocketManager requestSocketOpen]; + + UITapGestureRecognizer *gesture = + [[UITapGestureRecognizer alloc] initWithTarget:[Pastelog class] + action:@selector(submitLogs)]; + gesture.numberOfTapsRequired = 8; + [self.window addGestureRecognizer:gesture]; + }); + RTCInitializeSSL(); + }]; + }); + DDLogInfo(@"%@ applicationDidBecomeActive completed.", self.tag); }