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

Commit

Permalink
Merge pull request #470 from dtweston/add_memory_support_for_extensions
Browse files Browse the repository at this point in the history
Add low memory and OS kill support for extensions
  • Loading branch information
Benjamin Scholtysik (Reimold) authored Oct 20, 2017
2 parents 7362236 + c8ad9bc commit c9cf976
Showing 1 changed file with 48 additions and 14 deletions.
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

0 comments on commit c9cf976

Please sign in to comment.