使用 NSMigrationManager 手动 CoreData 迁移以附加数据(预加载)

发布于 2024-10-05 18:10:11 字数 2760 浏览 0 评论 0原文

我的应用程序资源文件夹中有一个填充的 sqlite 数据库。启动时,我想使用该 sqlite 数据库的数据预加载 coredata-store。我在 persistantStoreCoordinator 方法中使用 NSMigrationManager。这在第一次时效果很好,并将数据附加到存储中。但它会在每次启动时再次追加数据,因此第二次启动后数据将重复。我该如何解决这个问题?在数据库中我会使用主键,数据模型中是否有类似的东西?或者我可以比较实体对象吗?

谢谢四位的帮助,下面是我使用的方法:

- (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_;
}

I have a populated sqlite database in my app reousrce-folder. On startup I want to preload coredata-store with the data of this sqlite db. I use the NSMigrationManager in the persistantStoreCoordinator method. This works great at the first time and will append the data to the store. But it will append the data on each startup again, so the data will be duplicated, after the second startup. How can I solve this? In a database I would use primary keys, is there something similar in the data model? Or can I compare the entity-objects?

Thanks four your help, below the method I use:

- (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_;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捂风挽笑 2024-10-12 18:10:11

如果文档文件夹中存在默认文件,所提供的代码将合并该文件。如果合并文件后删除该文件,则不应每次都加载该文件。您可以设置一个用户默认标志来记录之前是否已合并。

更好的解决方案是使用默认数据创建一个核心数据持久存储并将其包含在应用程序包中。首次启动时,将该文件复制到文档文件夹,然后将其分配给持久存储。该方法会更快,并且您不必担心合并失败。

The code as provided will merge the default file if it is present in the documents folder. If you delete the file after you merge it, then it shouldn't load every time. You could set a user default flag to record if it had been previously merged.

A better solution would be to create a Core Data persistent store with the default data and include that in the app bundle. Upon first launch, copy that file to the documents folder and just assign it to the persistent store. That method would be faster and you wouldn't have to worry about the merge failing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文