我想从一个sqlite文件(已使用核心数据与模型A创建)附加到我的应用程序使用的另一个sqlite存储(它使用相同的型号A)。这个想法是快速导入大量的数据。使用NSMigrationManager将数据追加到核心数据持久性存储
我面临的问题是下面的代码只能工作一次。当我尝试两次运行相同的代码时,应用程序将在我的评论标记的行中崩溃。任何帮助将不胜感激。
NSError **err;
NSURL *importURL = [NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"import.sqlite"]];
NSURL *storeURL = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"applicationdata.sqlite"]];
NSMigrationManager *migrator = [[NSMigrationManager alloc] initWithSourceModel:[self managedObjectModel] destinationModel:[self managedObjectModel]];
NSMappingModel *mappingModel = [NSMappingModel inferredMappingModelForSourceModel:[self managedObjectModel] destinationModel:[self managedObjectModel] error:err];
NSError **err2;
// the following line crashes when the whole block is ran twice
[migrator migrateStoreFromURL:importURL
type:NSSQLiteStoreType
options:nil
withMappingModel:mappingModel
toDestinationURL:storeURL
destinationType:NSSQLiteStoreType
destinationOptions:nil
error:err2];
NSLog(@"import finished");
[migrator release];