From aa3402b53519ff9a45ea66260de53898431f0094 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 11 Apr 2017 17:38:06 -0400 Subject: [PATCH] Respond to CR. // FREEBIE --- src/Account/TSAccountManager.m | 7 ++++- src/Network/WebSockets/TSSocketManager.m | 39 +++++++++++++++--------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/Account/TSAccountManager.m b/src/Account/TSAccountManager.m index 8bd816fa9..f745ddcb4 100644 --- a/src/Account/TSAccountManager.m +++ b/src/Account/TSAccountManager.m @@ -271,7 +271,12 @@ NSString *const kNSNotificationName_RegistrationStateDidChange = @"kNSNotificati DDLogInfo(@"%@ Successfully unregistered", self.tag); success(); - // The success handler should reset local storage. + // This is called from `[SettingsTableViewController proceedToUnregistration]` whose + // success handler calls `[Environment resetAppData]`. + // This method, after calling that success handler, fires + // `kNSNotificationName_RegistrationStateDidChange` which is only safe to fire after + // the data store is reset. + [[NSNotificationCenter defaultCenter] postNotificationName:kNSNotificationName_RegistrationStateDidChange object:nil userInfo:nil]; diff --git a/src/Network/WebSockets/TSSocketManager.m b/src/Network/WebSockets/TSSocketManager.m index bf190e808..3a436c6c4 100644 --- a/src/Network/WebSockets/TSSocketManager.m +++ b/src/Network/WebSockets/TSSocketManager.m @@ -83,6 +83,8 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification"; // notification. @property (nonatomic) BOOL appIsActive; +@property (nonatomic) BOOL hasObservedNotifications; + @end #pragma mark - @@ -105,6 +107,27 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification"; [self addObserver:self forKeyPath:@"status" options:0 context:kSocketStatusObservationContext]; + OWSSingletonAssert(); + + return self; +} + +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + +// We want to observe these notifications lazily to avoid accessing +// the data store in [application: didFinishLaunchingWithOptions:]. +- (void)observeNotificationsIfNecessary +{ + if (self.hasObservedNotifications) { + return; + } + self.hasObservedNotifications = YES; + + self.appIsActive = [UIApplication sharedApplication].applicationState == UIApplicationStateActive; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification @@ -117,15 +140,6 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification"; selector:@selector(registrationStateDidChange:) name:kNSNotificationName_RegistrationStateDidChange object:nil]; - - OWSSingletonAssert(); - - return self; -} - -- (void)dealloc -{ - [[NSNotificationCenter defaultCenter] removeObserver:self]; } + (instancetype)sharedManager { @@ -461,11 +475,6 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification"; [TSStorageManager serverAuthToken]]; } -- (void)setFetchingTaskIdentifier:(UIBackgroundTaskIdentifier)fetchingTaskIdentifier -{ - _fetchingTaskIdentifier = fetchingTaskIdentifier; -} - #pragma mark UI Delegates - (void)observeValueForKeyPath:(NSString *)keyPath @@ -590,6 +599,8 @@ NSString *const SocketConnectingNotification = @"SocketConnectingNotification"; + (void)requestSocketOpen { DispatchMainThreadSafe(^{ + [[self sharedManager] observeNotificationsIfNecessary]; + // If the app is active and the user is registered, this will // simply open the websocket. //