Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 502
Jack Nutkins Avatar asked Mar 25 '12 16:03

Jack Nutkins


1 Answers

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];
}
like image 159
NeverBe Avatar answered Nov 11 '22 14:11

NeverBe