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

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

问题描述

我试图想出一个方法,我可以使用 UIManagedDocument 预先加载数据到核心数据。到目前为止,我尝试使用此代码在Loader应用程序中创建文档。

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 (实际上是一个bundle /文件夹本身),它应该看起来像这样:



如果不这样做,则文档的内容将直接驻留在应用程序集中,不能像您发现的 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天全站免登陆