-1

I am new to NSCache, I want to cache the response data and use it. So my [cache setobject] is in class A and I need to get it from class B using [cache objectForKey]. But it is always NULL.It works in same class as below..

NSURL *url = [NSURL URLWithString:@"http://ios.eezytutorials.com/sample-files/sample-dictionary-plist.plist"];
NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfURL:url];
NSCache *cache = [[NSCache alloc]init];
[cache setObject:dict forKey:url];
NSLog(@"%@",[cache objectForKey:url]);

How to get that from different class?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83

1 Answers1

1

NSCache is not a singleton or shared instance. If you want to use the contents of a specific instance, you could place your NSCache instance on AppDelegate or create your own singleton class to manage it.

FormigaNinja
  • 1,571
  • 1
  • 24
  • 36
  • Thanks. I tried , extended NSCache.. created singleton but still getting NULL.. MyDataCache* _cache = [MyDataCache sharedQDataCache]; //MyRequestDataModel* prevCachedData = [_cache objectForKey:[self.url absoluteString]]; NSLog(@"%@",[_cache objectForKey:[self.url absoluteString]]); – Rajkumar Gurunathan May 08 '15 at 09:52
  • Could you update your question with this singleton implementation? Maybe I can help you with this. – FormigaNinja May 08 '15 at 10:00