Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed a bunch of bugs that occured when I actually compiled those las…

…t few commits. Fixed a bug that caused NSUserDefaults to save too many times too fast, causing slow downs in the app.
  • Loading branch information...
commit a54a6aebfbd60e920417946ff862d9494359bddc 1 parent 83c6675
@shnhrrsn shnhrrsn authored
Showing with 16 additions and 11 deletions.
  1. +16 −11 EGOCache.m
View
27 EGOCache.m
@@ -11,21 +11,19 @@
static NSString* _EGOCacheDirectory;
static inline NSString* EGOCacheDirectory() {
- @synchronized(self) {
- if(!EGOCacheDirectory) {
- #ifdef TARGET_OS_IPHONE
- _EGOCacheDirectory = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/EGOCache"] retain];
- #else
- NSString* appSupportDir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
- _EGOCacheDirectory = [[[appSupportDir stringByAppendingPathComponent:[[NSProcessInfo processInfo] processName]] stringByAppendingPathComponent:@"EGOCache"] retain];
- #endif
- }
+ if(!_EGOCacheDirectory) {
+#ifdef TARGET_OS_IPHONE
+ _EGOCacheDirectory = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/EGOCache"] retain];
+#else
+ NSString* appSupportDir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
+ _EGOCacheDirectory = [[[appSupportDir stringByAppendingPathComponent:[[NSProcessInfo processInfo] processName]] stringByAppendingPathComponent:@"EGOCache"] retain];
+#endif
}
return _EGOCacheDirectory;
}
-static inline NSString* cachePathForKey(key) {
+static inline NSString* cachePathForKey(NSString* key) {
return [EGOCacheDirectory() stringByAppendingPathComponent:key];
}
@@ -87,7 +85,10 @@ - (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeI
[data writeToFile:cachePathForKey(key) atomically:YES];
[cacheDictionary setObject:[NSDate dateWithTimeIntervalSinceNow:timeoutInterval] forKey:key];
[[NSUserDefaults standardUserDefaults] setObject:cacheDictionary forKey:@"EGOCache"];
- [[NSUserDefaults standardUserDefaults] synchronize];
+
+ // 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];
}
- (NSData*)dataForKey:(NSString*)key {
@@ -98,6 +99,10 @@ - (NSData*)dataForKey:(NSString*)key {
}
}
+- (void)saveCacheDictionary {
+ [[NSUserDefaults standardUserDefaults] synchronize];
+}
+
#pragma mark -
#pragma mark String methods
Please sign in to comment.
Something went wrong with that request. Please try again.