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
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,6 @@
F40B874316AA233500676BB2 /* Frameworks */,
F40B874416AA233500676BB2 /* Resources */,
5142A2F8286744A694BDB5BA /* Upload Bugsnag dSYM */,
765F7485E02A01DA5E42AC27 /* [CP] Embed Pods Frameworks */,
E142138E1656CA8499F1E0A3 /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand Down Expand Up @@ -409,36 +407,6 @@
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
765F7485E02A01DA5E42AC27 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Bugsnag Test App/Pods-Bugsnag Test App-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
E142138E1656CA8499F1E0A3 /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Bugsnag Test App/Pods-Bugsnag Test App-resources.sh\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
Expand Down
8 changes: 4 additions & 4 deletions examples/objective-c-ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
PODS:
- Bugsnag (5.15.3)
- Bugsnag (5.16.0)

DEPENDENCIES:
- Bugsnag (from `../..`)

EXTERNAL SOURCES:
Bugsnag:
:path: ../..
:path: "../.."

SPEC CHECKSUMS:
Bugsnag: 49464c6c2fb7a2eb5a66064586c64a6e23454c04
Bugsnag: 47bcc70b43e3c616ec35d30c2ca94497b957199f

PODFILE CHECKSUM: 4c48f26cc704429f747c4af7a40e026b20fdc83e

COCOAPODS: 1.4.0
COCOAPODS: 1.5.0
8 changes: 4 additions & 4 deletions examples/objective-c-osx/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
PODS:
- Bugsnag (5.14.1)
- Bugsnag (5.16.0)

DEPENDENCIES:
- Bugsnag (from `../..`)

EXTERNAL SOURCES:
Bugsnag:
:path: ../..
:path: "../.."

SPEC CHECKSUMS:
Bugsnag: bc62bf437f55cfae29a4c836f29f9e8b64af3457
Bugsnag: 47bcc70b43e3c616ec35d30c2ca94497b957199f

PODFILE CHECKSUM: df3de31179198c85e1d68a7930eb894bbeb22744

COCOAPODS: 1.3.1
COCOAPODS: 1.5.0
32 changes: 0 additions & 32 deletions examples/objective-c-osx/objective-c-osx.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@
93BE1CCC1B62CC360016380C /* Frameworks */,
93BE1CCD1B62CC360016380C /* Resources */,
D24AA2BFC04EE1A1916426C8 /* Upload Bugsnag dSYM */,
3B550964C9496E4003E11717 /* [CP] Embed Pods Frameworks */,
2DB50B6F565C5C917EEF01B4 /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand Down Expand Up @@ -252,36 +250,6 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
2DB50B6F565C5C917EEF01B4 /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-objective-c-osx/Pods-objective-c-osx-resources.sh\"\n";
showEnvVarsInLog = 0;
};
3B550964C9496E4003E11717 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-objective-c-osx/Pods-objective-c-osx-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
AF12CC88EC2DA5EF6D40C883 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down
4 changes: 2 additions & 2 deletions examples/swift-ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Bugsnag (5.15.6)
- Bugsnag (5.16.0)

DEPENDENCIES:
- Bugsnag (from `../..`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../.."

SPEC CHECKSUMS:
Bugsnag: ff5f5e3059e6a9c9d27a899f3bf3774067553483
Bugsnag: 47bcc70b43e3c616ec35d30c2ca94497b957199f

PODFILE CHECKSUM: 2107babfbfdb18f0288407b9d9ebd48cbee8661c

Expand Down
32 changes: 0 additions & 32 deletions examples/swift-ios/bugsnag-example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@
D175F4B51ACDBD81009AFFB7 /* Frameworks */,
D175F4B61ACDBD81009AFFB7 /* Resources */,
9AFEEED933DC82BFDDB374F6 /* Upload Bugsnag dSYM */,
C54E80312CD2DFF73A2EEB6C /* [CP] Embed Pods Frameworks */,
AF034E316E834BBEE8E209A4 /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand Down Expand Up @@ -295,36 +293,6 @@
shellScript = "fork do\n Process.setsid\n STDIN.reopen(\"/dev/null\")\n STDOUT.reopen(\"/dev/null\", \"a\")\n STDERR.reopen(\"/dev/null\", \"a\")\n\n require 'shellwords'\n\n Dir[\"#{ENV[\"DWARF_DSYM_FOLDER_PATH\"]}/*/Contents/Resources/DWARF/*\"].each do |dsym|\n system(\"curl -F dsym=@#{Shellwords.escape(dsym)} -F projectRoot=#{Shellwords.escape(ENV[\"PROJECT_DIR\"])} https://upload.bugsnag.com/\")\n end\nend\n";
showEnvVarsInLog = 0;
};
AF034E316E834BBEE8E209A4 /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-bugsnag-example/Pods-bugsnag-example-resources.sh\"\n";
showEnvVarsInLog = 0;
};
C54E80312CD2DFF73A2EEB6C /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-bugsnag-example/Pods-bugsnag-example-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
Expand Down