次のエラーが表示されます:操作を完了できませんでした。(Cocoa エラー 134140。) *キャッチされない例外 'NSInvalidArgumentException' が原因でアプリを終了します。理由: '指定された永続ストアが見つかりませんでした。'
新しいバージョンのデータ モデルに変更したところです。ただし、以前のバージョンのデータ モデルを有効にすると、アプリは正常に動作します。新しいバージョンをアクティブ化すると、そのエラーが発生します。私が行った唯一の変更は、自動移行機能で処理できるように、データ モデルですべての変数を非オプションにしたことです。[fileManager copyItemAtPath:defaultStorePath toPath:myPathDocs error:&error] によってエラーがスローされないのに、永続ストアが見つからないという理由がわかりません。
これは、メイン バンドルの sqlite ファイルがデータ モデルの古いバージョンのものであるという事実と関係があると思われますが、新しいバージョンの sqlite ファイルはありません。ただし、自動移行は単に新しいモデル用の新しいファイルを独自に生成し (?)、既存のファイルからデータを移行するだけだという印象を受けました。これは間違っていますか?
私の iOS アプリでは、リソースに Formats.sqlite というファイルがあります。クリーン インストール後の起動時に、アプリは、アプリの /Documents フォルダーに既存の Formats.sqlite があるかどうかを確認します。存在しない場合は、メイン バンドルからそこにコピーします。モデルの新しいバージョンを作成する前は、これはすべて正常に機能していました。そのために使用するコードは次のとおりです。
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
int count = [[persistentStoreCoordinator persistentStores] count];
if (persistentStoreCoordinator != nil && count) {
return persistentStoreCoordinator;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:@"Formats.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
if (![fileManager fileExistsAtPath:myPathDocs]) {
NSLog(@"SQLite file not found, copying SQLITE file");
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Formats" ofType:@"sqlite"];
if (defaultStorePath) {
if(![fileManager copyItemAtPath:defaultStorePath toPath:myPathDocs error:&error]) {
NSLog(@"Error copying file at defaultStorePath to the documents path: %@",error);
}
}
}
storeURL = [NSURL fileURLWithPath:myPathDocs];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
nil];
if (persistentStoreCoordinator == nil) {
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
}
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{//my error code here}
そのため、「ここに私のエラーコード」というエラーがスローされます。これを修正するにはどうすればよいですか? なぜこうなった?ちなみに、コンソールは移行のすべての手順を吐き出すので、それをやろうとしているように見えますが、永続ストアが見つかりません。ビルド ターゲットに追加されました。
ユーザーがアプリの以前のバージョンからアップグレードした場合、データが移行され、すべてを再入力する必要がないようにしたいだけです. ありがとう!