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;
}