Add multitasking support by cleaning the memory when app is sent to background

This commit is contained in:
Olivier Poitrey 2010-06-08 14:38:55 +02:00
parent 58a24963b5
commit 4fedcee49e
1 changed files with 13 additions and 21 deletions

View File

@ -15,18 +15,6 @@ static SDImageCache *instance;
@implementation SDImageCache
#pragma mark SDImageCache (notification handlers)
- (void)didReceiveMemoryWarning:(void *)object
{
[self clearMemory];
}
- (void)willTerminate
{
[self cleanDisk];
}
#pragma mark NSObject
- (id)init
@ -51,14 +39,24 @@ static SDImageCache *instance;
// Subscribe to app events
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didReceiveMemoryWarning:)
selector:@selector(clearMemory)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willTerminate)
selector:@selector(cleanDisk)
name:UIApplicationWillTerminateNotification
object:nil];
UIDevice *device = [UIDevice currentDevice];
if ([device respondsToSelector:@selector(isMultitaskingSupported)] && device.multitaskingSupported)
{
// When in background, clean memory in order to have less chance to be killed
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(clearMemory)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
}
return self;
@ -70,13 +68,7 @@ static SDImageCache *instance;
[diskCachePath release], diskCachePath = nil;
[cacheInQueue release], cacheInQueue = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationWillTerminateNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}