我有一个预先填充好的 sqlite 文件,我将它复制到我的项目中(在文件夹中,然后到 xcode 项目窗口中)
当我用终端检查 sqlite 文件时,它工作正常,我的 sqlite 文件中有正确的数据。
但后来我尝试用来自 sqlite 文件的数据填充我的 tableView,但 tableView 仍然是空的。
你能告诉我我应该在哪里看吗?
我首先尝试了一些数据并且它可以工作(在 applicationDidFinish... 中有评论),但是对于我的 sqlite 文件,它不起作用:
这是我的代码:(这里是教程网址: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_;
}
非常感谢