只是想知道是否有其他人遇到过这种情况。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