核心数据迁移:文件存在

时间:2010-06-23 17:13:05

标签: iphone core-data

在iPhone上执行轻量级Core Data迁移时,我收到了一些用户的以下错误:

{
NSUnderlyingError = Error Domain=NSPOSIXErrorDomain Code=17 UserInfo=0x2991d0
"Operation could not be completed. File exists";
destinationURL = file://localhost/var/mobile/Applications/AEFD8CE2-0AF6-4227-AB84-73E2F5D83F26/Documents/App.sqlite.new;
reason = "Can't copy source store to destination store path";
sourceURL = file://localhost/var/mobile/Applications/AEFD8CE2-0AF6-4227-AB84-73E2F5D83F26/Documents/App.sqlite;
}

这是中断迁移的结果吗?还是其他一些原因?在任何情况下,在尝试迁移之前手动删除.new数据库是否是适当的补救措施?

代码只是一个简单的轻量级迁移:

NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"App.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
// convert automatically from prior models
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], 
                         NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES],
                         NSInferMappingModelAutomaticallyOption,
                         nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) 
{
    [MainAppDelegate printDetailedErrors:error];
}

更新:来自Apple论坛的BenT告诉我这是一个3.1.x的错误,但我还没有回过头来用我的错误日志数据验证这一点。

2 个答案:

答案 0 :(得分:3)

核心数据不会定期命名文件.new。你能展示产生这个错误的代码吗?

更新

如果BenT说这是一个错误,那就是答案。如果有人有核心数据问题的答案,Ben会这样做。

答案 1 :(得分:3)

在写了我对Marcus S. Zarra的回答之后,我做了一些研究并在CocoaBuilder上遇到Jerry Krinock's post,他意识到代码NSPOSIXErrorDomain是由底层BSD层函数引发的错误。并且当尝试覆盖现有文件时,根据Waikato Linux Users Group Posix引发错误17。 我的结论是:当迁移过程中出现问题并且您最终在应用程序支持目录中的应用程序文件夹中同时包含a和.new文件时,您会遇到困难,因为每次addPersistentStoreWithType:尝试创建用于合并的存储(。它会遇到Posix错误代码17.在确保所有设置都正确后,解决方案是删除.new并让迁移运行。它做了什么。