0

I have an app that has an entity named Holidays. I need to pre-populate this with a few years worth of holidays for my app.

I'm thinking that I could check the Holidays entity and load it at runtime by placing code in the AppDelegate didFinishLaunchingWithOptions method...I could just check and see if it already had records in it and then if not, add them in.

Is there a better way to do this?

Also, I tried doing a simple fetchrequest on the entity to count the records (as a way to see if it had already been loaded) but kept getting errors that my array was empty. How can I check to see if an entity is empty without erroring out?

This of course dies, but it's what I tried:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    // set up the default data

    //holiday dates
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Holidays" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSError *error = nil;
    NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"The entity is empty");
    }
    else {

        NSLog(@"The entity is loaded");
    }

    return YES;
}
Mark
  • 113
  • 1
  • 8
  • You also need to consider the possibility that the user might delete all these records and then restart the app, in which case you don't want to repopulate the records. To address this I have a single master table (entity) in core data which I use to keep track of the lifecycle. This is not a table the user can mess with, its used by the App. In this I keep a number of user preferences as well as status for things like tracking whether the database needs to be populated with seed data. – Duncan Groenewald Nov 05 '13 at 22:44
  • If you are planning on using iCloud with Core Data then you also need some strategy for handling the situation when a devices is creating a new document that already exists in iCloud (having already been initialised on some other device). In this case you probably don't want to be populating with any initial data. – Duncan Groenewald Nov 05 '13 at 22:48
  • Actually I am planning on utilizing iCloud, so this will be an issue. As for the data itself, I'm not planning on allowing the user to delete the data, I'm only storing the holidays so that I can check against them and give the user an alert if the date they choose is a holiday. I'm still not sure what is the best way to handle this. I appreciate all the info. – Mark Nov 06 '13 at 13:33

1 Answers1

1

That's "two questions in one", so I am going to answer the second :-)

executeFetchRequest returns nil if an error occurred. So your check should look like:

NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
    // report error
} else if ([fetchedObjects count] == 0) {
    NSLog(@"The entity is empty");
}
else {
    NSLog(@"The entity is loaded");
}

(For pre-populating your database, have a look at Any way to pre populate core data?.)

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382