使用 UIManagedDocument 在 iOS 5 中预加载核心数据数据库 [英] Pre-load core data database in iOS 5 with UIManagedDocument

查看:14
本文介绍了使用 UIManagedDocument 在 iOS 5 中预加载核心数据数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试想出一种方法,可以在使用 UIManagedDocument 的同时将数据预加载到核心数据中.到目前为止,我的尝试是使用此代码在加载程序"应用程序中制作文档..

I'm trying to come up with a way that I can pre-load data into core data while using an UIManagedDocument. My attempt so far is to make the document in a "Loader" app using this code..

NSURL *url  = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
                                                      inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"Default Database"];
if(![[NSFileManager defaultManager] fileExistsAtPath:[self.document.fileURL path]]){
    [self.document saveToURL:self.document.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){
        if(success)[self loadDataIntoDocument];
    }];
}

然后将在模拟器目录中创建的documents"目录中的persistentStore文件复制到将使用加载的数据库的主应用程序的Xcode资源部分.

and then copy the persistentStore file from the 'documents' directory that is created in the simulator's directory to the resources section in Xcode for the main app that will be using the loaded database.

我的问题是我不知道如何从应用程序包中复制该文档并将其成功用作文档.

My problem is that I can't figure out how to copy that document from the app bundle and use it as a document successfully.

我已经尝试从应用程序包中按原样复制文档目录,并尝试将其作为文档访问,这给出了 UIManagedDocument 只能访问文件包的错误.我尝试在主应用程序中创建另一个新文档,并从包中复制persistentStore 复制到在文档中创建的具有相同错误的文档上.我已经尝试使用 UIManagedDocument-(BOOL)loadFromContents:ofType:error: 方法..我什至不确定这是我应该使用的.

I've tried copying the document directory as-is from the app bundle and trying to access it as a document which gave the error that UIManagedDocument can only access a package of files. I've tried creating another fresh document in the main app and copying the persistentStore from the bundle over the one that is created in the document with the same error. And I've tried using UIManagedDocument's -(BOOL)loadFromContents:ofType:error: method .. I'm not even sure this is what I should be using.

有没有人知道这通常是如何完成的?谢谢!

Does anyone have an idea as to how this is normally done? Thanks!

推荐答案

我认为您不应该自己在 iOS 中处理文件和目录.

I don't think you should mess around with the files and directories yourself in iOS.

但是,您可以直接从捆绑包中打开文档.为此,您只需将在 Loader 应用程序中创建的整个文档目录复制到最终包中.结构应该比较简单,只有两个目录,里面有persistentStore文件.

You can, however, open the document directly from your bundle. For that, you simply have to copy the whole document directory you created in your Loader app into your final bundle. The structure should be rather simple, just two directories with the persistentStore file inside.

之后,当你想打开你的文档时,你需要注意两件事:你需要使用bundle目录作为基目录,并且你需要在最后添加一个/的文件"名称,所以 UIManagedDocument 知道目录是文档.代码如下所示:

Afterwards, when you want to open your document, you need to take care of two things: you need to use the bundle directory as the base directory and you need to add a / at the end of the "file" name, so UIManagedDocument knows that the directory is the document. The code looks like this:

NSURL* url = [[NSBundle mainBundle] bundleURL];
url = [url URLByAppendingPathComponent:@"filename/"];
UIManagedDocument* doc = [[UIManagedDocument alloc] initWithFileURL:url];

请注意,您不能写入该文件,因为您不能在应用程序包内写入.因此,如果您想要一个可写的文档,您需要将该文档复制到您的文档文件夹中.您只需将 doc 保存到其他位置即可轻松完成此操作:

Note that you cannot write to that file, as you are not allowed to write inside your application bundle. So if you want to have a writable document, you need to copy that document into your documents folder. You can do that easily by simply saving doc to a different location:

[doc saveToURL:newURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){}]

还有一件事:您必须确保在您的应用程序包中创建了文件夹结构.实现这一点的最简单方法是将整个文件夹拖到您的项目中,然后选择为任何添加的文件夹创建文件夹引用".对于文件"symbollist_en(实际上是一个包/文件夹本身),它应该如下所示:

如果您不这样做,文档的内容"将直接驻留在您的应用程序包中,正如您发现的那样,它无法作为 UIManagedDocument 打开.

One more thing: You have to make sure the folder structure is created in your app bundle. The easiest way to make this happen is to drag the whole folder into your project and then select "Create folder references for any added folders". For the "file" symbollist_en (which is actually a bundle/folder itself) it should look like this:

If you do not do this, the "contents" of the document will reside in your app bundle directly, which cannot be opened as a UIManagedDocument, as you found out.

这篇关于使用 UIManagedDocument 在 iOS 5 中预加载核心数据数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆