誰かがこれを見つけたのかどうか疑問に思うだけです。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.sqlite」がない場合はゼロから作成される空の「mydbという」。
- "mydb.sqlite"がメインバンドルに存在する場合は、指定されたディレクトリにコピーされると考えられます。
- "mydb.sqlite"が自分のxcdatamodelと互換性がない場合、アプリはクラッシュする必要があります。
ただしこれは既に作成済みのdbでのみ動作します。 たとえば、 "mydb.sqlite"という名前のランダムなDBをバンドルに入れて元のファイルを削除しようとすると、
アプリがクラッシュしません!!!空のdbが作成され、新しいdbは無視されます。 これは私のコードに反するので、これは完全に間違っています。
さらに、元のデータベースを追加しても何も起きず、アプリケーションは空のdbを作成します。
(はい、私は、私のプロジェクトをきれいシムアプリを削除し、さらに任意の変更が発生する前にビルドフォルダを削除します!)
任意のアイデア?
あなたのエラーは何ですか?それを掲示できますか? – Raptor
エラーはありません。それは私のコードによるべきである方法を機能しないのちょうどの観察 –