Search code examples
ioscachingwatchkit

How to do in-memory and on disk data caching in watchkit extension?


I have an ios app which sends data in key value pairs to watchkit extension, so now I want to cache that key value pairs both in memory and on disk at watchkit extension. So what is the best way to do this?


Solution

  • Because you're using key/value pairs, it seems straightforward to keep your data in a NSDictionary while in-memory. To persist the dictionary to disk:

    [myDictionary writeToFile:@"MyFile" atomically:YES];
    

    To load the dictionary back from disk:

    NSMutableDictionary *myDictionary =
      [NSMutableDictionary dictionaryWithContentsOfFile:@"MyFile"];
    

    For what it's worth, NSArray supports the same functionality.