Search code examples
iphoneobjective-cioscore-dataproperty-list

Pre-Populating Core Data with plist on applications first launch only


I have a plist file which is an array of dictionaries. Where each dictionary contains a set of strings. Each dictionary represents a celebrity.

What I would like to do is populate Core Data with the contents of this plist on the applications first launch, after which I would like to somehow check core data for the existence of my data, and if there is data, load it from there, otherwise load the initial data from the plist file again.

I know its possible to populate core data from a plist, but is what I'm suggesting a viable solution? Or is there a better approach?

Jack


Solution

  • My sample code

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    if (![defaults objectForKey:@"dataImported"]) {
    
        NSString *path = [[NSBundle mainBundle] pathForResource:@"dict" ofType:@"plist"];
        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
        for (NSString *key in [dict allKeys]) {
            NSDictionary *node = [dict objectForKey:key];
    
            MyClass *newObj = .....
        }
    
       [defaults setObject:@"OK" forKey:@"dataImported"];
       [defaults synchronize];
    }