I'm using Core Data and my app works perfectly in the simulator but on the actual device the database is empty.
I enabled CoreData logging, so I see the actual tables being created, and queries run but all the queries are empty. I also copied the app from the device and verified that the schema is created but with no data.
What am I missing for the data to actually get re-populated?
UPDATED WITH CODE
- (NSDictionary*)migrationOptions {
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
return options;
}
- (NSManagedObjectContext*)managedObjectContext {
if( _managedObjectContext != nil ) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator: coordinator];
[_managedObjectContext setUndoManager:nil];
[_managedObjectContext setRetainsRegisteredObjects:YES];
}
return _managedObjectContext;
}
- (NSManagedObjectModel*)managedObjectModel {
if( _managedObjectModel != nil ) {
return _managedObjectModel;
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"MyApp" ofType:@"momd"];
NSURL *momURL = [NSURL fileURLWithPath:path];
//_managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator*)persistentStoreCoordinator {
if( _persistentStoreCoordinator != nil ) {
return _persistentStoreCoordinator;
}
NSString* storePath = [self storePath];
NSURL *storeUrl = [self storeUrl];
NSError* error;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
NSDictionary* options = [self migrationOptions];
// Check whether the store already exists or not.
NSFileManager* fileManager = [NSFileManager defaultManager];
BOOL exists = [fileManager fileExistsAtPath:storePath];
TTDINFO(storePath);
if( !exists ) {
_modelCreated = YES;
} else {
if( _resetModel ||
[[NSUserDefaults standardUserDefaults] boolForKey:@"erase_all_preference"] ) {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"erase_all_preference"];
[fileManager removeItemAtPath:storePath error:nil];
_modelCreated = YES;
}
}
if (![_persistentStoreCoordinator
addPersistentStoreWithType: kStoreType
configuration: nil
URL: storeUrl
options: options
error: &error
]) {
// We couldn't add the persistent store, so let's wipe it out and try again.
[fileManager removeItemAtPath:storePath error:nil];
_modelCreated = YES;
if (![_persistentStoreCoordinator
addPersistentStoreWithType: kStoreType
configuration: nil
URL: storeUrl
options: nil
error: &error
]) {
// Something is terribly wrong here.
}
}
return _persistentStoreCoordinator;
}
- (NSString*)storePath {
return [[self applicationDocumentsDirectory] stringByAppendingPathComponent: kStoreFilename];
}
- (NSURL*)storeUrl {
return [NSURL fileURLWithPath:[self storePath]];
}