0
我想從sqlite文件預填充coredata數據庫,但我不明白爲什麼數據庫未填充。我的sqlite文件正確地填充了我需要的所有數據並添加到捆綁包資源中。Coredata數據庫預填充未填充
這裏persistentStoreCoordinator方法的int的AppDelegate:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (persistentStoreCoordinator != nil)
{
return persistentStoreCoordinator;
}
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingString:@"MyApp.db"];
NSError *error = nil;
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:storePath])
{
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"MyApp" ofType:@"db"];
if (defaultStorePath)
{
[fm copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error])
{
NSLog(@"Unresolved error : %@: %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;
}
在視圖控制器,我嘗試獲取的條目,但它似乎enter code here
空:
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
initWithEntityName:@"Type_textimage_4text"];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *description = [NSEntityDescription entityForName:@"Type_textimage_4text" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:description];
NSMutableArray *questions = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
NSLog(@"question : %@", questions);
我可能錯過了一些東西,但我不找不到什麼。任何人都可以幫助我嗎?
我有2個應用程序與預填充sqlite數據庫交付。我在外部數據庫管理器上創建了它們。如果你想看看DB上發生了什麼,你可以通過模擬器存儲所有文件的文件夾並從那裏獲取數據庫。檢查它與外部應用程序,你會看到裏面有什麼,獨立於你的代碼。然後你會知道你的問題在哪裏。 – Marcal
感謝您的回答。我用sqlite manager打開了數據庫,並且看到了我放入的所有條目的表格,但是我看到了其他我認爲由CoreData創建的表格以大寫形式寫入。例如,我有一個由我創建的表type_textimage_4text,其中所有的值和由CoreData創建的ZTYPE_TEXTIMAGE_4TEXT表爲空。 – Nonouf
如何將我現有的所有數據庫導入到核心數據中? – Nonouf