@@ -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}
0 commit comments