Skip to content
This repository has been archived by the owner on Feb 19, 2020. It is now read-only.

Add low memory and OS kill support for extensions #470

Merged
Changes from all 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
62 changes: 48 additions & 14 deletions Classes/BITCrashManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,14 @@ - (void) registerObservers {
__weak typeof(self) weakSelf = self;

if(nil == self.appDidBecomeActiveObserver) {
self.appDidBecomeActiveObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification
NSNotificationName name = UIApplicationDidBecomeActiveNotification;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpartial-availability"
if (bit_isRunningInAppExtension() && &NSExtensionHostDidBecomeActiveNotification != NULL) {
name = NSExtensionHostDidBecomeActiveNotification;
}
#pragma clang diagnostic pop
self.appDidBecomeActiveObserver = [[NSNotificationCenter defaultCenter] addObserverForName:name
object:nil
queue:NSOperationQueue.mainQueue
usingBlock:^(NSNotification __unused *note) {
Expand Down Expand Up @@ -504,7 +511,14 @@ - (void) registerObservers {
}

if (nil == self.appDidEnterBackgroundObserver) {
self.appDidEnterBackgroundObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification
NSNotificationName name = UIApplicationDidEnterBackgroundNotification;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpartial-availability"
if (bit_isRunningInAppExtension() && &NSExtensionHostDidEnterBackgroundNotification != NULL) {
name = NSExtensionHostDidEnterBackgroundNotification;
}
#pragma clang diagnostic pop
self.appDidEnterBackgroundObserver = [[NSNotificationCenter defaultCenter] addObserverForName:name
object:nil
queue:NSOperationQueue.mainQueue
usingBlock:^(NSNotification __unused *note) {
Expand All @@ -514,7 +528,14 @@ - (void) registerObservers {
}

if (nil == self.appWillEnterForegroundObserver) {
self.appWillEnterForegroundObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification
NSNotificationName name = UIApplicationWillEnterForegroundNotification;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpartial-availability"
if (bit_isRunningInAppExtension() && &NSExtensionHostWillEnterForegroundNotification != NULL) {
name = NSExtensionHostWillEnterForegroundNotification;
}
#pragma clang diagnostic pop
self.appWillEnterForegroundObserver = [[NSNotificationCenter defaultCenter] addObserverForName:name
object:nil
queue:NSOperationQueue.mainQueue
usingBlock:^(NSNotification __unused *note) {
Expand All @@ -524,17 +545,30 @@ - (void) registerObservers {
}

if (nil == self.appDidReceiveLowMemoryWarningObserver) {
self.appDidReceiveLowMemoryWarningObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification
object:nil
queue:NSOperationQueue.mainQueue
usingBlock:^(NSNotification __unused *note) {
// we only need to log this once
if (!self.didLogLowMemoryWarning) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kBITAppDidReceiveLowMemoryNotification];
self.didLogLowMemoryWarning = YES;

}
}];
if (bit_isRunningInAppExtension()) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 0, DISPATCH_MEMORYPRESSURE_WARN|DISPATCH_MEMORYPRESSURE_CRITICAL, dispatch_get_main_queue());
dispatch_source_set_event_handler(source, ^{
if (!self.didLogLowMemoryWarning) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kBITAppDidReceiveLowMemoryNotification];
self.didLogLowMemoryWarning = YES;
}
});
});
} else {
self.appDidReceiveLowMemoryWarningObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification
object:nil
queue:NSOperationQueue.mainQueue
usingBlock:^(NSNotification __unused *note) {
// we only need to log this once
if (!self.didLogLowMemoryWarning) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kBITAppDidReceiveLowMemoryNotification];
self.didLogLowMemoryWarning = YES;

}
}];
}
}
}

Expand Down