只是想知道是否有其他人遇到過這種情況。coredata無法在xcode 4.3上工作?
我得到了這段代碼,曾經在以前的xcode版本中工作輝煌。
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"mydb.sqlite"];
/*
Set up the store.
For the sake of illustration, provide a pre-populated default store.
*/
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn't exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"mydb" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
[self addSkipBackupAttributeToItemAtURL:storeUrl];
NSError *error;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
Typical reasons for an error here include:
* The persistent store is not accessible
* The schema for the persistent store is incompatible with current managed object model
Check the error message to determine what the actual problem was.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;
}
我期望從這一如下:
- 一個空的「mydb的」從頭開始創建,如果在我的包沒有「mydb.sqlite」。
- 如果一個「mydb.sqlite」存在於我的主包中,那麼我會期望它被複制到指定的diretory中。
- 如果「mydb.sqlite」與我的xcdatamodel不兼容,則應用程序必須崩潰。
但是,這隻適用於以前已經創建的數據庫。 例如,如果我嘗試把一個名爲「mydb.sqlite」的隨機數據庫放到我的包中,然後刪除原來的一個,那麼,
該應用程序不會崩潰!一個空白數據庫被創建並且新的數據庫被忽略。 這是完全錯誤的,因爲它違背了我的代碼。
此外,如果我加回原來的數據庫沒有任何反應,應用程序只是創建一個空白數據庫。
(是的,我做我的清理項目,刪除SIM卡的應用程序,並且發生任何改變之前甚至刪除build文件夾!)
任何想法?
你的錯誤是什麼?你可以把它發佈出來嗎? – Raptor 2012-02-26 10:26:46
沒有錯誤。這只是觀察不能按照我的代碼 – 2012-02-26 10:28:16