Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated EGOCache to fix some issues with lock ups when saving the NSU…

…serDefaults too fast
  • Loading branch information...
commit 718b6ebc942f78801e53c45442831a0593dd5d36 1 parent 895546b
@shnhrrsn shnhrrsn authored
Showing with 12 additions and 7 deletions.
  1. +1 −1  EGOCache.h
  2. +11 −6 EGOCache.m
View
2  EGOCache.h
@@ -41,4 +41,4 @@
- (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
#endif
-@end
+@end
View
17 EGOCache.m
@@ -75,7 +75,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];
@@ -98,11 +98,13 @@ - (void)setData:(NSData*)data forKey:(NSString*)key {
- (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval {
[data writeToFile:cachePathForKey(key) atomically:YES];
[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 {
@@ -114,7 +116,10 @@ - (NSData*)dataForKey:(NSString*)key {
}
- (void)saveCacheDictionary {
- [[NSUserDefaults standardUserDefaults] synchronize];
+ @synchronized(self) {
+ [[NSUserDefaults standardUserDefaults] setObject:cacheDictionary forKey:@"EGOCache"];
+ [[NSUserDefaults standardUserDefaults] synchronize];
+ }
}
#pragma mark -
@@ -174,4 +179,4 @@ - (void)dealloc {
[super dealloc];
}
-@end
+@end
Please sign in to comment.
Something went wrong with that request. Please try again.