Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deregister notification observers and listeners before application termination #301

Merged
merged 6 commits into from
Aug 2, 2018
Merged
31 changes: 28 additions & 3 deletions Source/BugsnagNotifier.m
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,13 @@ - (void)start {

[self setupConnectivityListener];
[self updateAutomaticBreadcrumbDetectionSettings];

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[self watchLifecycleEvents:center];

#if TARGET_OS_TV
[self.details setValue:@"tvOS Bugsnag Notifier" forKey:BSGKeyName];
[self addTerminationObserver:UIApplicationWillTerminateNotification];

#elif TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
[self.details setValue:@"iOS Bugsnag Notifier" forKey:BSGKeyName];
Expand Down Expand Up @@ -339,6 +340,8 @@ - (void)start {

[self batteryChanged:nil];
[self orientationChanged:nil];
[self addTerminationObserver:UIApplicationWillTerminateNotification];

#elif TARGET_OS_MAC
[self.details setValue:@"OSX Bugsnag Notifier" forKey:BSGKeyName];

Expand All @@ -351,6 +354,8 @@ - (void)start {
selector:@selector(willEnterBackground:)
name:NSApplicationDidResignActiveNotification
object:nil];

[self addTerminationObserver:NSApplicationWillTerminateNotification];
#endif

_started = YES;
Expand All @@ -359,18 +364,38 @@ - (void)start {
[self willEnterForeground:self];
}

- (void)addTerminationObserver:(NSString *)name {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(stop:)
name:name
object:nil];
}

/**
* Removes observers and listeners to prevent allocations when the app is terminated
*/
- (void)stop:(id)sender {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd name this a bit more verbosely, like unsubscribeFromNotifications. There is an off chance that with a name like stop, it could be picked up erroneously in app review as a private API usage.

[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.networkReachable stopWatchingConnectivity];

#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
[UIDevice currentDevice].batteryMonitoringEnabled = NO;
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
#endif
}

- (void)watchLifecycleEvents:(NSNotificationCenter *)center {
NSString *foregroundName;
NSString *backgroundName;

#if TARGET_OS_TV || TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
foregroundName = UIApplicationWillEnterForegroundNotification;
backgroundName = UIApplicationWillEnterForegroundNotification;
#elif TARGET_OS_MAC
foregroundName = NSApplicationWillBecomeActiveNotification;
backgroundName = NSApplicationDidFinishLaunchingNotification;
#endif

[center addObserver:self
selector:@selector(willEnterForeground:)
name:foregroundName
Expand Down