Fix removing observers correctly

Thanks to @idpaterson for reporting this in https://github.com/bitstadium/HockeySDK-iOS/pull/64
This commit is contained in:
Andreas Linde 2013-09-29 17:14:31 +02:00
parent 2aae127ae6
commit a4124dbda4
3 changed files with 132 additions and 45 deletions

View File

@ -58,8 +58,11 @@
NSString *_feedbackDir; NSString *_feedbackDir;
NSString *_settingsFile; NSString *_settingsFile;
id _appDidBecomeActiveObserver;
id _appDidEnterBackgroundObserver;
id _networkDidBecomeReachableObserver;
BOOL _incomingMessagesAlertShowing; BOOL _incomingMessagesAlertShowing;
BOOL _didSetupDidBecomeActiveNotifications;
BOOL _didEnterBackgroundState; BOOL _didEnterBackgroundState;
BOOL _networkRequestInProgress; BOOL _networkRequestInProgress;
} }
@ -78,7 +81,6 @@
_showFirstRequiredPresentationModal = YES; _showFirstRequiredPresentationModal = YES;
_disableFeedbackManager = NO; _disableFeedbackManager = NO;
_didSetupDidBecomeActiveNotifications = NO;
_networkRequestInProgress = NO; _networkRequestInProgress = NO;
_incomingMessagesAlertShowing = NO; _incomingMessagesAlertShowing = NO;
_lastCheck = nil; _lastCheck = nil;
@ -110,9 +112,7 @@
} }
- (void)dealloc { - (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:BITHockeyNetworkDidBecomeReachableNotification object:nil]; [self unregisterObservers];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
} }
@ -138,19 +138,51 @@
} }
} }
- (void)setupDidBecomeActiveNotifications { #pragma mark - Observers
if (!_didSetupDidBecomeActiveNotifications) { - (void) registerObservers {
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; __weak typeof(self) weakSelf = self;
[dnc addObserver:self selector:@selector(didEnterBackgroundActions) name:UIApplicationDidEnterBackgroundNotification object:nil]; if(nil == _appDidEnterBackgroundObserver) {
[dnc addObserver:self selector:@selector(didBecomeActiveActions) name:UIApplicationDidBecomeActiveNotification object:nil]; _appDidEnterBackgroundObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification
[dnc addObserver:self selector:@selector(didBecomeActiveActions) name:BITHockeyNetworkDidBecomeReachableNotification object:nil]; object:nil
_didSetupDidBecomeActiveNotifications = YES; queue:NSOperationQueue.mainQueue
usingBlock:^(NSNotification *note) {
typeof(self) strongSelf = weakSelf;
[strongSelf didEnterBackgroundActions];
}];
}
if(nil == _appDidBecomeActiveObserver) {
_appDidBecomeActiveObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification
object:nil
queue:NSOperationQueue.mainQueue
usingBlock:^(NSNotification *note) {
typeof(self) strongSelf = weakSelf;
[strongSelf didBecomeActiveActions];
}];
}
if(nil == _networkDidBecomeReachableObserver) {
_networkDidBecomeReachableObserver = [[NSNotificationCenter defaultCenter] addObserverForName:BITHockeyNetworkDidBecomeReachableNotification
object:nil
queue:NSOperationQueue.mainQueue
usingBlock:^(NSNotification *note) {
typeof(self) strongSelf = weakSelf;
[strongSelf didBecomeActiveActions];
}];
} }
} }
- (void)cleanupDidBecomeActiveNotifications { - (void) unregisterObservers {
[[NSNotificationCenter defaultCenter] removeObserver:self name:BITHockeyNetworkDidBecomeReachableNotification object:nil]; if(_appDidEnterBackgroundObserver) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:_appDidEnterBackgroundObserver];
_appDidEnterBackgroundObserver = nil;
}
if(_appDidBecomeActiveObserver) {
[[NSNotificationCenter defaultCenter] removeObserver:_appDidBecomeActiveObserver];
_appDidBecomeActiveObserver = nil;
}
if(_networkDidBecomeReachableObserver) {
[[NSNotificationCenter defaultCenter] removeObserver:_networkDidBecomeReachableObserver];
_networkDidBecomeReachableObserver = nil;
}
} }
#pragma mark - Private methods #pragma mark - Private methods
@ -206,7 +238,7 @@
- (void)startManager { - (void)startManager {
if ([self isFeedbackManagerDisabled]) return; if ([self isFeedbackManagerDisabled]) return;
[self setupDidBecomeActiveNotifications]; [self registerObservers];
// we are already delayed, so the notification already came in and this won't invoked twice // we are already delayed, so the notification already came in and this won't invoked twice
switch ([[UIApplication sharedApplication] applicationState]) { switch ([[UIApplication sharedApplication] applicationState]) {

View File

@ -47,7 +47,8 @@
BOOL _updateAlertShowing; BOOL _updateAlertShowing;
BOOL _lastCheckFailed; BOOL _lastCheckFailed;
BOOL _didSetupDidBecomeActiveNotifications; id _appDidBecomeActiveObserver;
id _networkDidBecomeReachableObserver;
} }
@ -67,18 +68,39 @@
} }
} }
- (void)setupDidBecomeActiveNotifications { #pragma mark - Observers
if (!_didSetupDidBecomeActiveNotifications) {
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; - (void) registerObservers {
[dnc addObserver:self selector:@selector(didBecomeActiveActions) name:UIApplicationDidBecomeActiveNotification object:nil]; __weak typeof(self) weakSelf = self;
[dnc addObserver:self selector:@selector(didBecomeActiveActions) name:BITHockeyNetworkDidBecomeReachableNotification object:nil]; if(nil == _appDidBecomeActiveObserver) {
_didSetupDidBecomeActiveNotifications = YES; _appDidBecomeActiveObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification
object:nil
queue:NSOperationQueue.mainQueue
usingBlock:^(NSNotification *note) {
typeof(self) strongSelf = weakSelf;
[strongSelf didBecomeActiveActions];
}];
}
if(nil == _networkDidBecomeReachableObserver) {
_networkDidBecomeReachableObserver = [[NSNotificationCenter defaultCenter] addObserverForName:BITHockeyNetworkDidBecomeReachableNotification
object:nil
queue:NSOperationQueue.mainQueue
usingBlock:^(NSNotification *note) {
typeof(self) strongSelf = weakSelf;
[strongSelf didBecomeActiveActions];
}];
} }
} }
- (void)cleanupDidBecomeActiveNotifications { - (void) unregisterObservers {
[[NSNotificationCenter defaultCenter] removeObserver:self name:BITHockeyNetworkDidBecomeReachableNotification object:nil]; if(_appDidBecomeActiveObserver) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:_appDidBecomeActiveObserver];
_appDidBecomeActiveObserver = nil;
}
if(_networkDidBecomeReachableObserver) {
[[NSNotificationCenter defaultCenter] removeObserver:_networkDidBecomeReachableObserver];
_networkDidBecomeReachableObserver = nil;
}
} }
@ -90,7 +112,6 @@
_updateAvailable = NO; _updateAvailable = NO;
_lastCheckFailed = NO; _lastCheckFailed = NO;
_enableStoreUpdateManager = NO; _enableStoreUpdateManager = NO;
_didSetupDidBecomeActiveNotifications = NO;
_updateAlertShowing = NO; _updateAlertShowing = NO;
_updateUIEnabled = YES; _updateUIEnabled = YES;
_newStoreVersion = nil; _newStoreVersion = nil;
@ -114,9 +135,7 @@
} }
- (void)dealloc { - (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:BITHockeyNetworkDidBecomeReachableNotification object:nil]; [self unregisterObservers];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
} }
@ -374,7 +393,7 @@
self.lastCheck = [NSDate distantPast]; self.lastCheck = [NSDate distantPast];
} }
[self setupDidBecomeActiveNotifications]; [self registerObservers];
// we are already delayed, so the notification already came in and this won't invoked twice // we are already delayed, so the notification already came in and this won't invoked twice
switch ([[UIApplication sharedApplication] applicationState]) { switch ([[UIApplication sharedApplication] applicationState]) {

View File

@ -59,6 +59,10 @@ typedef NS_ENUM(NSInteger, BITUpdateAlertViewTag) {
BOOL _lastCheckFailed; BOOL _lastCheckFailed;
BOOL _sendUsageData; BOOL _sendUsageData;
id _appDidBecomeActiveObserver;
id _appDidEnterBackgroundObserver;
id _networkDidBecomeReachableObserver;
BOOL _didSetupDidBecomeActiveNotifications; BOOL _didSetupDidBecomeActiveNotifications;
BOOL _didEnterBackgroundState; BOOL _didEnterBackgroundState;
@ -111,21 +115,55 @@ typedef NS_ENUM(NSInteger, BITUpdateAlertViewTag) {
} }
} }
- (void)setupDidBecomeActiveNotifications { #pragma mark - Observers
- (void) registerObservers {
__weak typeof(self) weakSelf = self;
if(nil == _appDidEnterBackgroundObserver) {
_appDidEnterBackgroundObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification
object:nil
queue:NSOperationQueue.mainQueue
usingBlock:^(NSNotification *note) {
typeof(self) strongSelf = weakSelf;
[strongSelf didEnterBackgroundActions];
}];
}
if(nil == _appDidBecomeActiveObserver) {
_appDidBecomeActiveObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification
object:nil
queue:NSOperationQueue.mainQueue
usingBlock:^(NSNotification *note) {
typeof(self) strongSelf = weakSelf;
[strongSelf didBecomeActiveActions];
}];
}
if(nil == _networkDidBecomeReachableObserver) {
_networkDidBecomeReachableObserver = [[NSNotificationCenter defaultCenter] addObserverForName:BITHockeyNetworkDidBecomeReachableNotification
object:nil
queue:NSOperationQueue.mainQueue
usingBlock:^(NSNotification *note) {
typeof(self) strongSelf = weakSelf;
[strongSelf didBecomeActiveActions];
}];
}
if (!_didSetupDidBecomeActiveNotifications) { if (!_didSetupDidBecomeActiveNotifications) {
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc addObserver:self selector:@selector(didEnterBackgroundActions) name:UIApplicationDidEnterBackgroundNotification object:nil];
[dnc addObserver:self selector:@selector(didBecomeActiveActions) name:UIApplicationDidBecomeActiveNotification object:nil];
[dnc addObserver:self selector:@selector(didBecomeActiveActions) name:BITHockeyNetworkDidBecomeReachableNotification object:nil];
_installationIdentification = [self stringValueFromKeychainForKey:kBITUpdateInstallationIdentification]; _installationIdentification = [self stringValueFromKeychainForKey:kBITUpdateInstallationIdentification];
_didSetupDidBecomeActiveNotifications = YES; _didSetupDidBecomeActiveNotifications = YES;
} }
} }
- (void)cleanupDidBecomeActiveNotifications { - (void) unregisterObservers {
[[NSNotificationCenter defaultCenter] removeObserver:self name:BITHockeyNetworkDidBecomeReachableNotification object:nil]; if(_appDidEnterBackgroundObserver) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:_appDidEnterBackgroundObserver];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil]; _appDidEnterBackgroundObserver = nil;
}
if(_appDidBecomeActiveObserver) {
[[NSNotificationCenter defaultCenter] removeObserver:_appDidBecomeActiveObserver];
_appDidBecomeActiveObserver = nil;
}
if(_networkDidBecomeReachableObserver) {
[[NSNotificationCenter defaultCenter] removeObserver:_networkDidBecomeReachableObserver];
_networkDidBecomeReachableObserver = nil;
}
} }
@ -161,7 +199,7 @@ typedef NS_ENUM(NSInteger, BITUpdateAlertViewTag) {
} }
// the UI is now blocked, make sure we don't add our UI on top of it over and over again // the UI is now blocked, make sure we don't add our UI on top of it over and over again
[self cleanupDidBecomeActiveNotifications]; [self unregisterObservers];
} }
} }
@ -391,10 +429,8 @@ typedef NS_ENUM(NSInteger, BITUpdateAlertViewTag) {
} }
- (void)dealloc { - (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:BITHockeyNetworkDidBecomeReachableNotification object:nil]; [self unregisterObservers];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
[_urlConnection cancel]; [_urlConnection cancel];
@ -681,7 +717,7 @@ typedef NS_ENUM(NSInteger, BITUpdateAlertViewTag) {
[self performSelector:@selector(checkForUpdate) withObject:nil afterDelay:1.0f]; [self performSelector:@selector(checkForUpdate) withObject:nil afterDelay:1.0f];
} }
} }
[self setupDidBecomeActiveNotifications]; [self registerObservers];
} }