Permalink
Browse files

Merge remote branch 'enormego/master'

  • Loading branch information...
Luke Redpath
Luke Redpath committed Nov 1, 2009
2 parents dc7f945 + 718b6eb commit ec86582a718b1942db8750ac97fe8b1a6e51db6c
Showing with 12 additions and 7 deletions.
  1. +1 −1 EGOCache.h
  2. +11 −6 EGOCache.m
View
@@ -42,4 +42,4 @@
- (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
#endif
-@end
+@end
View
@@ -77,7 +77,7 @@ - (void)clearCache {
for(NSString* key in cacheDictionary) {
[[NSFileManager defaultManager] removeItemAtPath:cachePathForKey(key) error:NULL];
}
-
+
[cacheDictionary removeAllObjects];
[[NSUserDefaults standardUserDefaults] setObject:cacheDictionary forKey:@"EGOCache"];
[[NSUserDefaults standardUserDefaults] synchronize];
@@ -110,11 +110,13 @@ - (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeI
[diskWriteOperation release];
[cacheDictionary setObject:[NSDate dateWithTimeIntervalSinceNow:timeoutInterval] forKey:key];
- [[NSUserDefaults standardUserDefaults] setObject:cacheDictionary forKey:@"EGOCache"];
- // Prevents multiple-rapid user defaults saves from happening, which will slow down your app
+ [self performSelectorOnMainThread:@selector(saveAfterDelay) withObject:nil waitUntilDone:YES]; // Need to make sure the save delay get scheduled in the main runloop, not the current threads
+}
+
+- (void)saveAfterDelay { // Prevents multiple-rapid user defaults saves from happening, which will slow down your app
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(saveCacheDictionary) object:nil];
- [self performSelector:@selector(saveCacheDictionary) withObject:nil afterDelay:1.0];
+ [self performSelector:@selector(saveCacheDictionary) withObject:nil afterDelay:0.3];
}
- (NSData*)dataForKey:(NSString*)key {
@@ -131,7 +133,10 @@ - (void)writeData:(NSData*)data toPath:(NSString *)path;
}
- (void)saveCacheDictionary {
- [[NSUserDefaults standardUserDefaults] synchronize];
+ @synchronized(self) {
+ [[NSUserDefaults standardUserDefaults] setObject:cacheDictionary forKey:@"EGOCache"];
+ [[NSUserDefaults standardUserDefaults] synchronize];
+ }
}
#pragma mark -
@@ -192,4 +197,4 @@ - (void)dealloc {
[super dealloc];
}
-@end
+@end

0 comments on commit ec86582

Please sign in to comment.