2010-12-05 8 views
2

私のアプリケーションのreousrce-folderにpopulated sqliteデータベースがあります。起動時に、このsqlite dbのデータをcoredata-storeにプリロードします。 NSMigrationManagerは、persistantStoreCoordinatorメソッドで使用します。この機能は初めてのことであり、データを店舗に追加します。ただし、2回目の起動後にデータが複製されるように、各起動時にデータが再度追加されます。これをどうすれば解決できますか?データベースでは主キーを使用しますが、データモデルには類似したものがありますか?あるいは、エンティティオブジェクトを比較できますか?データを追加するためのNSMigrationManagerを使用した手動CoreDataマイグレーション(プリロード)

おかげで4人のあなたの助けは、私が使用する方法の下:それはドキュメントフォルダに存在する場合

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

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"Raetselspass.sqlite"]; 
NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; 
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Raetselspass" ofType:@"sqlite"]; 
NSURL *defaultStoreUrl = [NSURL fileURLWithPath:defaultStorePath]; 

/* 
    Set up the store. 
    For the sake of illustration, provide a pre-populated default store. 
    */ 
     // CANNOT USE THIS BELOW: WILL WORK ONCE, BUT WHEN I WILL UPDATE THE APP WITH 
     // NEW DATA TO APPEND, THEN THIS WILL NOT WORK 
// NSFileManager *fileManager = [NSFileManager defaultManager]; 
// If the expected store doesn’t exist, copy the default store. 
// if (![fileManager fileExistsAtPath:storePath]) { 
// if (defaultStorePath) { 
// [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL]; 
// } 
// } 

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]); 
    abort(); // Fail 
} 

//migration 
rror:&error]; 
NSError *err = nil; 
NSMigrationManager *migrator = [[NSMigrationManager alloc] initWithSourceModel:[self managedObjectModel] destinationModel:[self managedObjectModel]]; 
NSMappingModel *mappingModel = [NSMappingModel inferredMappingModelForSourceModel:[self managedObjectModel] destinationModel:[self managedObjectModel] error:&err]; 
NSError *err2; 
if (![migrator migrateStoreFromURL:defaultStoreUrl 
      type:NSSQLiteStoreType 
      options:nil 
     withMappingModel:mappingModel 
     toDestinationURL:storeUrl 
     destinationType:NSSQLiteStoreType 
    destinationOptions:nil 
     error:&err2]) 
{ 
    //handle the error 
} 
NSLog(@"import finished"); 
[migrator release]; 

return persistentStoreCoordinator_; 
} 

答えて

2

提供されるコードは、デフォルトのファイルをマージします。ファイルをマージした後で削除すると、毎回ロードされるべきではありません。以前にマージされていた場合は、ユーザーのデフォルトフラグを記録するように設定できます。

より良い解決策は、デフォルトデータを使用してCore Data永続ストアを作成し、それをアプリケーションバンドルに含めることです。最初の起動時に、そのファイルをドキュメントフォルダにコピーし、永続ストアに割り当てます。その方法は高速になり、マージに失敗しても心配する必要はありません。

+0

「永続ストアに割り当てる」という意味を説明できますか?私はあなたの推薦がどのように違うのか混乱しています。 – Flaviu

関連する問題