【发布时间】:2012-02-26 10:25:31
【问题描述】:
只是想知道是否有人遇到过这个问题。
我得到了这段代码,它曾经在以前的 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.sqlite”,则从头开始创建一个空的“mydb”。
- 如果我的主包中存在“mydb.sqlite”,那么我希望它被复制到指定的目录中。
- 如果“mydb.sqlite”与我的 xcdatamodel 不兼容,则应用程序必须崩溃。
但这仅适用于之前已创建的数据库。 例如,如果我尝试将一个名为“mydb.sqlite”的随机数据库放入我的包中并删除原来的数据库,
应用程序不会崩溃!!!创建一个空白数据库并忽略新数据库。 这是完全错误的,因为它违反了我的代码。
此外,如果我添加回原始数据库,则没有任何反应,应用程序只会创建一个空白数据库。
(是的,我确实清理了我的项目,删除了 sim 应用程序,甚至在发生任何更改之前删除了构建文件夹!)
有什么想法吗??
【问题讨论】:
-
你的错误是什么?可以发一下吗?
-
没有错误。这只是观察到没有按照我的代码应有的方式运行