UIManagedDocument只能读取文件包的文档

时间:2011-12-09 13:50:57

标签: ios core-data icloud

我的应用正在使用核心数据SQLite数据库。我想让我的用户使用iCloud在设备之间进行同步 - 我以为我可以使用UIManagedDocument。

我按照Apple的文档对其进行了子类化,并且在需要创建新的持久性存储文件时它可以正常工作。但是,当我尝试使用它来打开我的旧持久存储文件时,我得到以下异常抛出错误:

“UIManagedDocument只能读取文件包的文档”

这是否意味着我需要将旧的持久存储迁移到由UIManagedDocument管理的新商店?如果是这样,我是否需要手动执行此操作(即,从旧商店一次一个地读取每条记录并将其写入新的记录中)?

提前致谢!

2 个答案:

答案 0 :(得分:5)

UIManagedDocument创建包(文件夹)而不是原子库。商店仍在那里,但它埋在包里。如果右键单击模拟器中Documents文件夹中创建的文件,您将能够看到结构。默认值为

mydocument.foo
   -> StoreContent
      -> persistentStore

您需要做的是为您的应用文件类型创建一个新扩展,例如,如果您的数据库扩展名为.myappdb,则需要在项目设置中创建一个新的文档类型,该类型可能是{{1} }。您可以复制.myappdbw

条目中的所有设置

接下来,您处理在.myappdb处打开旧文档而不是将其传递给持久性存储协调员时,您将创建上面的目录结构。

mydocumenturl

然后将旧数据库移动到您创建的文件夹系统

NSURL *newurl = [[mydocumenturl  URLByDeletingPathExtension] URLByAppendingPathExtension:@"myappdbw"];
NSURL *desturl = [newurl URLByAppendingPathComponent:@"StoreContent"];
[[NSFileManager defaultManager] createDirectoryAtURL:desturl withIntermediateDirectories:YES attributes:nil error:NULL];
NSURL *finalurl = [desturl URLByAppendingPathComponent:@"persistentStore"];

然后您可以将捆绑网址传递给UIManagedDocument

[[NSFileManager defaultManager] moveItemAtURL:mydocumenturl toURL:finalurl error:NULL];

对iCloud集成有用的链接是

http://developer.apple.com/library/ios/#releasenotes/DataManagement/RN-iCloudCoreData/_index.html

它有点神秘,因为大多数承诺的示例代码到目前为止都没有出现,但另一方面它的演绎主要相当简单。有关更多提示,请查看WWDC2011会议107,116和315。

但请注意,如果您要使用此方法迁移旧版文档 DONT ,请在迁移时设置UIManagedDocument *doc = [[UIManagedDocument alloc] initWithFileURL:newurl]; ,因为此时包会更改。上面的文档很好地描述了它。

答案 1 :(得分:1)

感谢您的提示。我想我找到了一个更简单的解决方案。

我只是创建一个新的UIManagedDocument,其文件名不同于旧的持久存储位置。

在我的子类UIManagedDocument中,我覆盖configurePersistentStoreCoordinatorForURL方法并在那里进行迁移:

- (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)storeURL ofType:(NSString *)fileType modelConfiguration:(NSString *)configuration storeOptions:(NSDictionary *)storeOptions error:(NSError **)error
{
    // If legacy store exists, copy it to the new location
    NSFileManager* fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:legacyPersistentStoreURL.path])
    {
        NSError* thisError = nil;
        [fileManager copyItemAtURL:legacyPersistentStoreURL toURL:storeURL error:&thisError];
        [fileManager removeItemAtURL:legacyPersistentStoreURL error:&thisError];
    }

    return [super configurePersistentStoreCoordinatorForURL:storeURL ofType:fileType modelConfiguration:configuration storeOptions:storeOptions error:error];
}
相关问题