0

約3.000レコードの非常に大規模なデータベースを使用しているアプリケーションがあります。以前は、アプリを初めて起動するときにデータベースをロードしていましたが、レコード数を使用して、時間を節約するために事前入力されたデータベースを実装しています。

私の質問は、アプリをアプリストアからデバイスに更新するときに何が起こっているのかということです。アプリはデータベースの更新バージョンがあることを認識し、新しいデータベースをロードするか、アプリですでにアクティブになっているデータベースを引き続き使用しますか?

アプリでこのコードを使用して、事前入力されたCoreDataDBを使用しています。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
  if (persistentStoreCoordinator != nil) {
    return persistentStoreCoordinator;
}

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"database.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:@"database" ofType:@"sqlite"];
    if (defaultStorePath) {
        [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
    }
}

NSURL *storeUrl = [NSURL fileURLWithPath:storePath];

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

NSError *error;
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
    // Update to handle the error appropriately.
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    exit(-1);  // Fail
}    

return persistentStoreCoordinator;

}

4

2 に答える 2

2

現在アプリ ストアにあるアプリのバージョンが 1.0 であるとします。このアプリは、最初の起動時に DB をダウンロードしました。ここで、アプリ自体に DB をバンドルする新しいバージョン 1.1 を公開します。

既存のアプリ (1.0) のユーザーが 1.1 にアップグレードする場合、何が起こるかは完全に制御できます。実際、バージョン 1.1 は、最初の起動時に、バージョン 1.0 がインストールしたユーザー ディレクトリに DB が存在するかどうかを確認できます。存在する場合、バージョン 1.1 は、リソース ディレクトリからコピーして DB をアップグレードする必要があることを認識します。

実際には、DB はいずれの場合もユーザー ディレクトリにコピーする必要があるため、ユーザー データが消去されないように確認することができます。

NSUserDefaults一般に、アプリの将来のバージョンごとに、それが新しいインストールに対するアップグレードかどうかを知る方法があるように、バージョン番号を に保存することがあります (バージョン番号が存在する場合は、その特定のバージョンからのアップグレードです) 、それ以外の場合は新規インストールです)。

于 2012-06-30T15:47:27.020 に答える
0

アプリを更新しても、既に作成されているファイルは引き続き存在するため、古いバージョンのアプリは、古いバージョンで作成した古いデータベースを引き続き使用します。

于 2012-06-30T14:18:25.727 に答える