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

Commit

Permalink
Detect memory pressure in extensions
Browse files Browse the repository at this point in the history
The UIApplicationDidReceiveMemoryWarningNotification isn't fired when
running in an extension, so we need to resort to a lower-level mechanism.
  • Loading branch information
dtweston committed Oct 16, 2017
1 parent 91a919c commit 3d5b3c7
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions Classes/BITCrashManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -542,17 +542,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 3d5b3c7

Please sign in to comment.