I need a dictionary to hold a limited number of objects that need to be created with an internet connection and cache them so they remain available when there's no internet connection.
NSCache
seems ideal since I can specify a limit or it will automatically adjust itself based on available memory, but I can't save the object to NSUserDefaults
. If I try caching like this, which worked for NSDictionary
:
+ (void)cacheUsers:(NSCache *)users {
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:users];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:kCacheID];
}
the app crashes: [NSCache encodeWithCoder:]: unrecognized selector sent to instance
Should I convert the NSCache
to an NSDictionary
before caching and then convert again when retrieving? Or is there a better way to save an NSCache
?