6

iPhoneで永続的なストアコーディネーターを作成する際のエラー処理に関する情報を見つけようとしています。軽量移行を実装しました

   NSError *error = nil;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options 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. 

     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.


     If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

     If you encounter schema incompatibility errors during development, you can reduce their frequency by:
     * Simply deleting the existing store:
     [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

     * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
     @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}

     Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}    

return _persistentStoreCoordinator;

これは、軽量移行のサポートが追加されたAppleのコードに基づいています。

アプリケーションでエラーが発生する場合は、ここでエラーの処理に関する情報を見つけることができません。データベースが作成できない場合、アプリケーションはまったく使用できないように思われます。

  • アプリケーションを再インストールして関連情報を表示するようにユーザーに依頼するだけですか?
  • エラーに関するプロンプトを追加している間、abort()ステートメントを保持できますか、それともアプリケーションがAppleによって拒否される原因になりますか?
4

1 に答える 1

16

この状況でabort()を呼び出すことは問題外です。クラッシュしたアプリはすべてAppleによって拒否されます。また、問題は解決しません。アプリを再起動すると、同じストアファイルが見つかるため、再度失敗します。

同じ理由で、アプリを再インストールしても効果がなく、ユーザーエクスペリエンスが低下します。

もちろん、移行がテストされている場合は、この状況は発生しないはずです。ただし、この致命的なエラーが発生し、アプリがデータベースを開くことができない場合は、新しいデータベースを作成する必要があります。

実行する正確な手順は、データベースに保存されている内容と、データを回復できるかどうか/方法によって異なります。だからあなたは

  • で古いデータベースファイルを削除する[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]か、プログラムリソースからデフォルトのデータベースファイルをにコピーしますstoreURL
  • _persistentStoreCoordinator addPersistentStoreWithType:...もう一度呼び出して、新しいデータベースを開きます。
  • おそらく、サーバーからのデータ、またはデータを再作成するために実行する必要があることをデータベースに再度入力します。
于 2012-12-10T19:22:40.313 に答える