Skip to content

Commit ed4de7e

Browse files
committed
Simplify code / pre-cache localNumber
Now that localNumber is read from a dedicated dbConnection we don't have to worry about it blocking. // FREEBIE
1 parent f99d4e9 commit ed4de7e

File tree

6 files changed

+78
-125
lines changed

6 files changed

+78
-125
lines changed

Signal/src/AppDelegate.m

Lines changed: 49 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,13 @@ - (BOOL)application:(UIApplication *)application
394394
}
395395
DDLogInfo(@"Application opened with URL: %@", url);
396396

397-
[[TSAccountManager sharedInstance] ifRegistered:YES
398-
runAsync:^{
399-
dispatch_async(dispatch_get_main_queue(), ^{
400-
// Wait up to N seconds for database view registrations to
401-
// complete.
402-
[self showImportUIForAttachment:attachment remainingRetries:5];
403-
});
404-
}];
397+
if ([TSAccountManager isRegistered]) {
398+
dispatch_async(dispatch_get_main_queue(), ^{
399+
// Wait up to N seconds for database view registrations to
400+
// complete.
401+
[self showImportUIForAttachment:attachment remainingRetries:5];
402+
});
403+
}
405404

406405
return YES;
407406
} else {
@@ -455,70 +454,52 @@ - (void)applicationDidBecomeActive:(UIApplication *)application {
455454

456455
static dispatch_once_t onceToken;
457456
dispatch_once(&onceToken, ^{
457+
RTCInitializeSSL();
458458

459-
// At this point, potentially lengthy DB locking migrations could be running.
460-
// Avoid blocking app launch by putting all further possible DB access in async thread.
461-
[[TSAccountManager sharedInstance]
462-
ifRegistered:YES
463-
runAsync:^{
464-
DDLogInfo(@"%@ running post launch block for registered user: %@",
465-
self.tag,
466-
[TSAccountManager localNumber]);
467-
468-
RTCInitializeSSL();
469-
470-
[OWSSyncPushTokensJob runWithPushManager:[PushManager sharedManager]
471-
accountManager:[Environment getCurrent].accountManager
472-
preferences:[Environment preferences]
473-
showAlerts:NO];
474-
475-
// Clean up any messages that expired since last launch immediately
476-
// and continue cleaning in the background.
477-
[[OWSDisappearingMessagesJob sharedJob] startIfNecessary];
478-
479-
// Mark all "attempting out" messages as "unsent", i.e. any messages that were not successfully
480-
// sent before the app exited should be marked as failures.
481-
[[[OWSFailedMessagesJob alloc] initWithStorageManager:[TSStorageManager sharedManager]] run];
482-
[[[OWSFailedAttachmentDownloadsJob alloc] initWithStorageManager:[TSStorageManager sharedManager]]
483-
run];
484-
485-
[AppStoreRating setupRatingLibrary];
486-
}];
487-
488-
[[TSAccountManager sharedInstance]
489-
ifRegistered:NO
490-
runAsync:^{
491-
dispatch_async(dispatch_get_main_queue(), ^{
492-
DDLogInfo(@"%@ running post launch block for unregistered user.", self.tag);
493-
494-
// Unregistered user should have no unread messages. e.g. if you delete your account.
495-
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
496-
497-
[TSSocketManager requestSocketOpen];
498-
499-
UITapGestureRecognizer *gesture =
500-
[[UITapGestureRecognizer alloc] initWithTarget:[Pastelog class]
501-
action:@selector(submitLogs)];
502-
gesture.numberOfTapsRequired = 8;
503-
[self.window addGestureRecognizer:gesture];
504-
});
505-
RTCInitializeSSL();
506-
}];
507-
});
459+
if ([TSAccountManager isRegistered]) {
460+
// At this point, potentially lengthy DB locking migrations could be running.
461+
// Avoid blocking app launch by putting all further possible DB access in async block
462+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
463+
DDLogInfo(
464+
@"%@ running post launch block for registered user: %@", self.tag, [TSAccountManager localNumber]);
465+
466+
// Clean up any messages that expired since last launch immediately
467+
// and continue cleaning in the background.
468+
[[OWSDisappearingMessagesJob sharedJob] startIfNecessary];
469+
470+
// Mark all "attempting out" messages as "unsent", i.e. any messages that were not successfully
471+
// sent before the app exited should be marked as failures.
472+
[[[OWSFailedMessagesJob alloc] initWithStorageManager:[TSStorageManager sharedManager]] run];
473+
[[[OWSFailedAttachmentDownloadsJob alloc] initWithStorageManager:[TSStorageManager sharedManager]] run];
474+
475+
[AppStoreRating setupRatingLibrary];
476+
});
477+
} else {
478+
DDLogInfo(@"%@ running post launch block for unregistered user.", self.tag);
508479

509-
[[TSAccountManager sharedInstance]
510-
ifRegistered:YES
511-
runAsync:^{
512-
[TSSocketManager requestSocketOpen];
480+
// Unregistered user should have no unread messages. e.g. if you delete your account.
481+
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
513482

514-
dispatch_async(dispatch_get_main_queue(), ^{
515-
[[Environment getCurrent].contactsManager fetchSystemContactsIfAlreadyAuthorized];
516-
});
483+
[TSSocketManager requestSocketOpen];
484+
485+
UITapGestureRecognizer *gesture =
486+
[[UITapGestureRecognizer alloc] initWithTarget:[Pastelog class] action:@selector(submitLogs)];
487+
gesture.numberOfTapsRequired = 8;
488+
[self.window addGestureRecognizer:gesture];
489+
}
490+
}); // end dispatchOnce for first time we become active
517491

518-
// This will fetch new messages, if we're using domain
519-
// fronting.
520-
[[PushManager sharedManager] applicationDidBecomeActive];
521-
}];
492+
// Every time we become active...
493+
if ([TSAccountManager isRegistered]) {
494+
// At this point, potentially lengthy DB locking migrations could be running.
495+
// Avoid blocking app launch by putting all further possible DB access in async block
496+
dispatch_async(dispatch_get_main_queue(), ^{
497+
[TSSocketManager requestSocketOpen];
498+
[[Environment getCurrent].contactsManager fetchSystemContactsIfAlreadyAuthorized];
499+
// This will fetch new messages, if we're using domain fronting.
500+
[[PushManager sharedManager] applicationDidBecomeActive];
501+
});
502+
}
522503

523504
DDLogInfo(@"%@ applicationDidBecomeActive completed.", self.tag);
524505
}

Signal/src/environment/Migrations/OWS103EnableVideoCalling.m

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,27 @@ + (NSString *)migrationId
2121
- (void)runUp
2222
{
2323
DDLogWarn(@"%@ running migration...", self.tag);
24-
25-
// TODO: It'd be nice if TSAccountManager had a
26-
// [ifRegisteredRunAsync: ifNoRegisteredRunAsync:] method.
27-
[[TSAccountManager sharedInstance] ifRegistered:YES
28-
runAsync:^{
29-
TSUpdateAttributesRequest *request = [[TSUpdateAttributesRequest alloc]
30-
initWithUpdatedAttributesWithVoice];
31-
[[TSNetworkManager sharedManager] makeRequest:request
32-
success:^(NSURLSessionDataTask *task, id responseObject) {
33-
DDLogInfo(@"%@ successfully ran", self.tag);
34-
[self save];
35-
}
36-
failure:^(NSURLSessionDataTask *task, NSError *error) {
37-
if (!IsNSErrorNetworkFailure(error)) {
38-
OWSProdError([OWSAnalyticsEvents
39-
errorEnableVideoCallingRequestFailed]);
40-
}
41-
DDLogError(@"%@ failed with error: %@", self.tag, error);
42-
}];
43-
}];
44-
[[TSAccountManager sharedInstance] ifRegistered:NO
45-
runAsync:^{
46-
DDLogInfo(@"%@ skipping; not registered", self.tag);
47-
[self save];
48-
}];
24+
if ([TSAccountManager isRegistered]) {
25+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
26+
TSUpdateAttributesRequest *request = [[TSUpdateAttributesRequest alloc] initWithUpdatedAttributesWithVoice];
27+
[[TSNetworkManager sharedManager] makeRequest:request
28+
success:^(NSURLSessionDataTask *task, id responseObject) {
29+
DDLogInfo(@"%@ successfully ran", self.tag);
30+
[self save];
31+
}
32+
failure:^(NSURLSessionDataTask *task, NSError *error) {
33+
if (!IsNSErrorNetworkFailure(error)) {
34+
OWSProdError([OWSAnalyticsEvents errorEnableVideoCallingRequestFailed]);
35+
}
36+
DDLogError(@"%@ failed with error: %@", self.tag, error);
37+
}];
38+
});
39+
} else {
40+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
41+
DDLogInfo(@"%@ skipping; not registered", self.tag);
42+
[self save];
43+
});
44+
}
4945
}
5046

5147
#pragma mark - Logging

Signal/src/util/OWSContactsSyncing.m

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,9 @@ - (void)sendSyncContactsMessageIfPossible
139139
return;
140140
}
141141

142-
[[TSAccountManager sharedInstance] ifRegistered:YES
143-
runAsync:^{
144-
dispatch_async(dispatch_get_main_queue(), ^{
145-
[self sendSyncContactsMessageIfNecessary];
146-
});
147-
}];
142+
if ([TSAccountManager sharedInstance]) {
143+
[self sendSyncContactsMessageIfNecessary];
144+
}
148145
}
149146

150147
#pragma mark - Logging

SignalServiceKit/src/Account/TSAccountManager.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ extern NSString *const kNSNotificationName_LocalNumberDidChange;
3535
*/
3636
+ (BOOL)isRegistered;
3737

38-
- (void)ifRegistered:(BOOL)isRegistered runAsync:(void (^)())block;
39-
4038
/**
4139
* Returns current phone number for this device, which may not yet have been registered.
4240
*

SignalServiceKit/src/Account/TSAccountManager.m

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,6 @@ - (BOOL)isRegistered
9393
return _isRegistered;
9494
}
9595

96-
- (void)ifRegistered:(BOOL)runIfRegistered runAsync:(void (^)())block
97-
{
98-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
99-
if ([self isRegistered] == runIfRegistered) {
100-
if (runIfRegistered) {
101-
DDLogDebug(@"%@ Running existing-user block", self.tag);
102-
} else {
103-
DDLogDebug(@"%@ Running new-user block", self.tag);
104-
}
105-
block();
106-
} else {
107-
if (runIfRegistered) {
108-
DDLogDebug(@"%@ Skipping existing-user block for new-user", self.tag);
109-
} else {
110-
DDLogDebug(@"%@ Skipping new-user block for existing-user", self.tag);
111-
}
112-
}
113-
});
114-
}
115-
11696
- (void)didRegister
11797
{
11898
DDLogInfo(@"%@ didRegister", self.tag);

SignalServiceKit/src/Account/TSPreKeyManager.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,11 @@ + (void)checkPreKeysIfNecessary
109109
// condition.
110110
lastPreKeyCheckTimestamp = [NSDate date];
111111

112-
[[TSAccountManager sharedInstance] ifRegistered:YES
113-
runAsync:^{
114-
[TSPreKeyManager checkPreKeys];
115-
}];
112+
if ([TSAccountManager isRegistered]) {
113+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
114+
[TSPreKeyManager checkPreKeys];
115+
});
116+
}
116117
}
117118
});
118119
}

0 commit comments

Comments
 (0)