我的基于Core Data文档的应用程序(仅限10.5)的数据模型位于 框架中,因此使用核心数据映射 模型进行自动模式升级似乎不起作用。核心数据机构 似乎没有在 不在应用的主包中找到适当的数据模型或映射模型。因此,我正在运行 configurePersistentStoreCoordinatorForURL:ofType:...
我的 NSPersistenDocument
子类(以下代码)手动执行迁移,而不是使用自动迁移 。如果 迁移成功,我将永久存储 存储迁移到临时文件,然后覆盖现有文件。该文档然后显示 消息出现错误“自打开或保存该文档后,该文档的文件已被其他应用程序 更改。”当我尝试保存时。正如其他 列表中指出的那样,这是由于我修改了 文档的“背后”。我尝试更新文档的 文件修改日期,如下所示,但是我随后收到一条错误对话框 ,并提示“文档的位置”test.ovproj“不能被确定为 ”。当我尝试保存时。我不太确定这个 错误的原因,但在其他 交易一个不必要的消息(在这种情况下)是不是我想要的。没有“文档更改”警告的手动核心数据模式迁移?
任何人都可以提供一些指导?有没有办法手动升级 模式的文件的持久存储没有触发 这些(这个这种情况下不必要的)警告之一?在我的子类 -configurePersistentStoreCoordinatorForURL:ofType:...
升级数据存储
代码:
if(upgradeNeeded) {
NSManagedObjectModel *sourceModel = [NSManagedObjectModel mergedModelFromBundles:VUIModelBundles() orStoreMetadata:meta];
if(sourceModel == nil) {
*error = [NSError errorWithDomain:VUIErrorDomainn ode:VUICoreDataErrorCode localizedReason:BWLocalizedString(@"Unable to find original data model for project.")];
return NO;
}
NSManagedObjectModel *destinationModel = [self managedObjectModel];
NSMigrationManager *migrationManager = [[NSMigrationManager alloc] initWithSourceModel:sourceModel destinationModel:destinationModel];
NSMappingModel *mappingModel = [NSMappingModel mappingModelFromBundles:VUIModelBundles() forSourceModel:sourceModel destinationModel:destinationModel];
if(mappingModel == nil) {
*error = [NSError errorWithDomain:VUIErrorDomain code:VUICoreDataErrorCode localizedReason:BWLocalizedString(@"Unable to find mapping model to convert project to most recent project format.")];
return NO;
}
@try {
//move file to backup
NSAssert([url isFileURL], @"store url is not a file URL");
NSString *tmpPath = [NSString tempFilePath];
id storeType = [meta objectForKey:NSStoreTypeKey];
if(![migrationManager migrateStoreFromURL:url
type:storeType
options:storeOptions
withMappingModel:mappingModel
toDestinationURL:[NSURLfileURLWithPath:tmpPath]
destinationType:storeType
destinationOptions:storeOptions
error:error]) {
return NO;
} else {
//replace old with new
if(![[NSFileManager defaultManager] removeItemAtPath:[url path] error:error] ||
![[NSFileManager defaultManager] moveItemAtPath:tmpPath toPath:[url path] error:error]) {
return NO;
}
// update document file modification date to prevent warning (#292)
NSDate *newModificationDate = [[[NSFileManager defaultManager] fileAttributesAtPath:[url path] traverseLink:NO] bjectForKey:NSFileModificationDate];
[self setFileModificationDate:newModificationDate];
}
}
@finally {
[migrationManager release];
}
}
}
return [super configurePersistentStoreCoordinatorForURL:url ofType:fileType modelConfiguration:configuration storeOptions:storeOptions error:error];
甚至在设置文件修改日期之前设置文件修改日期有趣的是,readFromURL:ofType:error:并未解决问题。使用FSExchangeObjects(),以及使用[self setFileModificationDate:]和新文件修改日期确实有效。感谢指针。 – 2008-12-23 23:47:52