i have a well pre-populated sqlite file that i copied into my project (in the folder, and into the xcode project window)
when i check the sqlite file with the Terminal, it works fine, my sqlite file has the right data in it.
But then i try to fill my tableView with the data from the sqlite file, but the tableView is still empty.
Can you please tell me where i should look at?
i first tried with some data and it works (what is commented in applicationDidFinish…), but with my sqlite file, it does not work :
here’s my code : (and here’s the tutorial url : http://www.raywenderlich.com/980/core-data-tutorial-how-to-preloadimport-existing-data)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSManagedObjectContext *context = [self managedObjectContext];
/*
THIS WORKS :
FailedBankInfo *failedBankInfo = [NSEntityDescription insertNewObjectForEntityForName:@"FailedBankInfo"
inManagedObjectContext:context];
failedBankInfo.name = @"the name";
failedBankInfo.city = @"the city";
failedBankInfo.state = @"the state";
FailedBankDetails *failedBankDetails = [NSEntityDescription insertNewObjectForEntityForName:@"FailedBankDetails"
inManagedObjectContext:context];
failedBankDetails.closeDate = [NSDate date];
failedBankDetails.updatedDate = [NSDate date];
failedBankDetails.zip = [NSNumber numberWithInt:12345];
failedBankInfo.details = failedBankDetails;
failedBankDetails.info = failedBankInfo;
NSError *error;
if (![context save:&error]){
NSLog(@"%@,", [error localizedDescription]);
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"FailedBankInfo"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (FailedBankInfo *info in fetchedObjects){
NSLog(@"name : %@", info.name);
FailedBankDetails *details = info.details;
NSLog(@"zip : %@", details.zip);
}
[fetchRequest release];*/
FailedBanksListViewController *root = (FailedBanksListViewController *)[_navController topViewController];
root.context = [self managedObjectContext];
[window addSubview:_navController.view];
[self.window makeKeyAndVisible];
return YES;
}
/** Returns the persistent store coordinator for the application.
If the coordinator doesn't already exist, it is created and the application's store added to it.
*/
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"FailedBanksCD.sqlite"];
/*NSString *storePath = [[self applicationDocumentsDirectory]
stringByAppendingPathComponent: @"FailedBanksCD.sqlite"];
*/
//NSURL *storeURL = [NSURL fileURLWithPath:storePath];
// Put down default db if it doesn't already exist
NSString *storePath = [NSString stringWithFormat:@"%@", storeURL];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle]
pathForResource:@"FailedBanksCD" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
NSError *error = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator_;
}
Thanks a lot
To me it looks as if the database was not copied to the doc-dir. Can you move this part
beyond this if block:
You have to remove the current empty DB from the doc-dir before trying again (remove in simulator, delete app from device and re-install).