Simplify code / pre-cache localNumber

Now that localNumber is read from a dedicated dbConnection we don't have
to worry about it blocking.

// FREEBIE
pull/1/head
Michael Kirk 8 years ago
parent f99d4e9df9
commit ed4de7e8af

@ -394,14 +394,13 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
} }
DDLogInfo(@"Application opened with URL: %@", url); DDLogInfo(@"Application opened with URL: %@", url);
[[TSAccountManager sharedInstance] ifRegistered:YES if ([TSAccountManager isRegistered]) {
runAsync:^{ dispatch_async(dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_main_queue(), ^{ // Wait up to N seconds for database view registrations to
// Wait up to N seconds for database view registrations to // complete.
// complete. [self showImportUIForAttachment:attachment remainingRetries:5];
[self showImportUIForAttachment:attachment remainingRetries:5]; });
}); }
}];
return YES; return YES;
} else { } else {
@ -455,70 +454,52 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
RTCInitializeSSL();
// At this point, potentially lengthy DB locking migrations could be running. if ([TSAccountManager isRegistered]) {
// Avoid blocking app launch by putting all further possible DB access in async thread. // At this point, potentially lengthy DB locking migrations could be running.
[[TSAccountManager sharedInstance] // Avoid blocking app launch by putting all further possible DB access in async block
ifRegistered:YES dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
runAsync:^{ DDLogInfo(
DDLogInfo(@"%@ running post launch block for registered user: %@", @"%@ running post launch block for registered user: %@", self.tag, [TSAccountManager localNumber]);
self.tag,
[TSAccountManager localNumber]); // Clean up any messages that expired since last launch immediately
// and continue cleaning in the background.
RTCInitializeSSL(); [[OWSDisappearingMessagesJob sharedJob] startIfNecessary];
[OWSSyncPushTokensJob runWithPushManager:[PushManager sharedManager] // Mark all "attempting out" messages as "unsent", i.e. any messages that were not successfully
accountManager:[Environment getCurrent].accountManager // sent before the app exited should be marked as failures.
preferences:[Environment preferences] [[[OWSFailedMessagesJob alloc] initWithStorageManager:[TSStorageManager sharedManager]] run];
showAlerts:NO]; [[[OWSFailedAttachmentDownloadsJob alloc] initWithStorageManager:[TSStorageManager sharedManager]] run];
// Clean up any messages that expired since last launch immediately [AppStoreRating setupRatingLibrary];
// and continue cleaning in the background. });
[[OWSDisappearingMessagesJob sharedJob] startIfNecessary]; } else {
DDLogInfo(@"%@ running post launch block for unregistered user.", self.tag);
// 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);
// Unregistered user should have no unread messages. e.g. if you delete your account.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[TSSocketManager requestSocketOpen];
UITapGestureRecognizer *gesture =
[[UITapGestureRecognizer alloc] initWithTarget:[Pastelog class]
action:@selector(submitLogs)];
gesture.numberOfTapsRequired = 8;
[self.window addGestureRecognizer:gesture];
});
RTCInitializeSSL();
}];
});
[[TSAccountManager sharedInstance] // Unregistered user should have no unread messages. e.g. if you delete your account.
ifRegistered:YES [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
runAsync:^{
[TSSocketManager requestSocketOpen];
dispatch_async(dispatch_get_main_queue(), ^{ [TSSocketManager requestSocketOpen];
[[Environment getCurrent].contactsManager fetchSystemContactsIfAlreadyAuthorized];
}); UITapGestureRecognizer *gesture =
[[UITapGestureRecognizer alloc] initWithTarget:[Pastelog class] action:@selector(submitLogs)];
gesture.numberOfTapsRequired = 8;
[self.window addGestureRecognizer:gesture];
}
}); // end dispatchOnce for first time we become active
// This will fetch new messages, if we're using domain // Every time we become active...
// fronting. if ([TSAccountManager isRegistered]) {
[[PushManager sharedManager] applicationDidBecomeActive]; // At this point, potentially lengthy DB locking migrations could be running.
}]; // Avoid blocking app launch by putting all further possible DB access in async block
dispatch_async(dispatch_get_main_queue(), ^{
[TSSocketManager requestSocketOpen];
[[Environment getCurrent].contactsManager fetchSystemContactsIfAlreadyAuthorized];
// This will fetch new messages, if we're using domain fronting.
[[PushManager sharedManager] applicationDidBecomeActive];
});
}
DDLogInfo(@"%@ applicationDidBecomeActive completed.", self.tag); DDLogInfo(@"%@ applicationDidBecomeActive completed.", self.tag);
} }

@ -21,31 +21,27 @@ static NSString *const OWS103EnableVideoCallingMigrationId = @"103";
- (void)runUp - (void)runUp
{ {
DDLogWarn(@"%@ running migration...", self.tag); DDLogWarn(@"%@ running migration...", self.tag);
if ([TSAccountManager isRegistered]) {
// TODO: It'd be nice if TSAccountManager had a dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// [ifRegisteredRunAsync: ifNoRegisteredRunAsync:] method. TSUpdateAttributesRequest *request = [[TSUpdateAttributesRequest alloc] initWithUpdatedAttributesWithVoice];
[[TSAccountManager sharedInstance] ifRegistered:YES [[TSNetworkManager sharedManager] makeRequest:request
runAsync:^{ success:^(NSURLSessionDataTask *task, id responseObject) {
TSUpdateAttributesRequest *request = [[TSUpdateAttributesRequest alloc] DDLogInfo(@"%@ successfully ran", self.tag);
initWithUpdatedAttributesWithVoice]; [self save];
[[TSNetworkManager sharedManager] makeRequest:request }
success:^(NSURLSessionDataTask *task, id responseObject) { failure:^(NSURLSessionDataTask *task, NSError *error) {
DDLogInfo(@"%@ successfully ran", self.tag); if (!IsNSErrorNetworkFailure(error)) {
[self save]; OWSProdError([OWSAnalyticsEvents errorEnableVideoCallingRequestFailed]);
} }
failure:^(NSURLSessionDataTask *task, NSError *error) { DDLogError(@"%@ failed with error: %@", self.tag, error);
if (!IsNSErrorNetworkFailure(error)) { }];
OWSProdError([OWSAnalyticsEvents });
errorEnableVideoCallingRequestFailed]); } else {
} dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
DDLogError(@"%@ failed with error: %@", self.tag, error); DDLogInfo(@"%@ skipping; not registered", self.tag);
}]; [self save];
}]; });
[[TSAccountManager sharedInstance] ifRegistered:NO }
runAsync:^{
DDLogInfo(@"%@ skipping; not registered", self.tag);
[self save];
}];
} }
#pragma mark - Logging #pragma mark - Logging

@ -139,12 +139,9 @@ NSString *const kTSStorageManagerOWSContactsSyncingLastMessageKey =
return; return;
} }
[[TSAccountManager sharedInstance] ifRegistered:YES if ([TSAccountManager sharedInstance]) {
runAsync:^{ [self sendSyncContactsMessageIfNecessary];
dispatch_async(dispatch_get_main_queue(), ^{ }
[self sendSyncContactsMessageIfNecessary];
});
}];
} }
#pragma mark - Logging #pragma mark - Logging

@ -35,8 +35,6 @@ extern NSString *const kNSNotificationName_LocalNumberDidChange;
*/ */
+ (BOOL)isRegistered; + (BOOL)isRegistered;
- (void)ifRegistered:(BOOL)isRegistered runAsync:(void (^)())block;
/** /**
* Returns current phone number for this device, which may not yet have been registered. * Returns current phone number for this device, which may not yet have been registered.
* *

@ -93,26 +93,6 @@ NSString *const TSAccountManager_LocalRegistrationIdKey = @"TSStorageLocalRegist
return _isRegistered; return _isRegistered;
} }
- (void)ifRegistered:(BOOL)runIfRegistered runAsync:(void (^)())block
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if ([self isRegistered] == runIfRegistered) {
if (runIfRegistered) {
DDLogDebug(@"%@ Running existing-user block", self.tag);
} else {
DDLogDebug(@"%@ Running new-user block", self.tag);
}
block();
} else {
if (runIfRegistered) {
DDLogDebug(@"%@ Skipping existing-user block for new-user", self.tag);
} else {
DDLogDebug(@"%@ Skipping new-user block for existing-user", self.tag);
}
}
});
}
- (void)didRegister - (void)didRegister
{ {
DDLogInfo(@"%@ didRegister", self.tag); DDLogInfo(@"%@ didRegister", self.tag);

@ -109,10 +109,11 @@ static const NSTimeInterval kSignedPreKeyUpdateFailureMaxFailureDuration = 10 *
// condition. // condition.
lastPreKeyCheckTimestamp = [NSDate date]; lastPreKeyCheckTimestamp = [NSDate date];
[[TSAccountManager sharedInstance] ifRegistered:YES if ([TSAccountManager isRegistered]) {
runAsync:^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[TSPreKeyManager checkPreKeys]; [TSPreKeyManager checkPreKeys];
}]; });
}
} }
}); });
} }

Loading…
Cancel
Save