I am storing an array in NSCache.(My NSCache name is aesCache)
#define DEBUG_CACHE 1
self.aesCache = [[NSCache alloc] init];
[self.aesCache setDelegate:self];
To save an array in NSCache
- (void)saveObjects:(NSMutableArray*)savedObj {
[self saveValue:savedObj key:@"Objects"];
}
to save it to NSCache.
- (void)saveValue:(id)value key:(NSString *)key {
#ifdef DEBUG_CACHE
NSLog(@"#CACHE_MANAGER: SAVE Object");
NSLog(@"#CACHE_MANAGER: KEY %@", key);
#endif
if (value && key) {
[self.aesCache setObject:value forKey:key];
}
else {
#ifdef DEBUG_CACHE
NSLog(@"#CACHE_MANAGER: *** KEY VALUE SEEMS TO BE NIL ***");
#endif
}
}
To get that array from cache, I use:
- (NSMutableArray*)getObjects {
array = [self getValue:@"Objects"];
#ifdef DEBUG_CACHE
NSLog(@"#CACHE_MANAGER: Getting the 'Objects' from Cache");
#endif
if (areas == nil) {
#ifdef DEBUG_CACHE
NSLog(@"#CACHE_MANAGER: No Objects cached. ");
#endif
}
return array;
}
I’ll get the array from the cache.But if I Lock the screen and then open it
the cache delegate function
- (void)cache:(NSCache *)cache willEvictObject:(id)obj {
#ifdef DEBUG_CACHE
NSLog(@"#CACHE_MANAGER: Going to evict the object");
#endif
}
fired and I will get empty result.
How can I solve this issue.
Please help me out.
The aim of NSCache is to cache objets, but it can be flushed at any time to preserve memory. If you want a more persistant cache, you should use a dictionary.