我可以访问Core Data中用于外部二进制存储的文件吗? [英] Can I access the files used for external binary storage in Core Data?

查看:144
本文介绍了我可以访问Core Data中用于外部二进制存储的文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用媒体数据库应用程序。我有一个具有数据存储的自定义模型,并考虑将其重写到Core Data。我特别感兴趣的一个用例是电影存储。我将电影文件存储在数据库中,但媒体框架只能从文件(而不是数据)读取电影。

I am working on a media DB application. I have a custom model with data storage and think about rewriting it to Core Data. One use case that is of particular interest to me is movie storage. I store movie files in the DB, but the media framework can only read movies from files (not data).

Core Data提供了一个方便的功能称为外部二进制存储 ,其中实体数据不存储在DB中,而是存储在外部文件中。这对Core Data API用户是透明的。我的问题是,我可以获得外部文件的路径,以便我可以存储一个电影使用Core Data,然后很容易从其Core Data外部文件加载它

Core Data offers a handy feature called "external binary storage", where the entity data is not stored in the DB, but in an external file. This is transparent to the Core Data API user. My question is, can I get the path to the external file, so that I could store a movie using Core Data and then easily load it from its Core Data external file?

推荐答案

如果你想直接访问数据(即不是通过CoreData),你最好给每个文件一个UUID作为名称,并将该名称存储在数据库中,

If you want to access the data directly (i.e., not through CoreData), you may be better off giving each file a UUID as name, and store that name in the database, and store the actual file yourself.

如果你使用UIManagedDocument,你有几个选项。使用上述技术,您可以将文件存储在数据库旁边,因为UIManagedDocument实际上是一个文件包。

If you use UIManagedDocument, you have several options. Using the above technique, you can store the files alongside the database, because UIManagedDocument is really a file package.

或者,您可以从UIManagedDocument子类化并覆盖处理读/写额外文件。这将让您访问文件本身。

Alternatively, you can subclass from UIManagedDocument and override the methods that handle reading/writing the "extra" files. This will give you access to the files themselves. You can hook there to do whatever you want, including grabbing the actual URL to the file CoreData automatically creates.

- (id)additionalContentForURL:(NSURL *)absoluteURL error:(NSError **)error
- (BOOL)readAdditionalContentFromURL:(NSURL *)absoluteURL error:(NSError **)error
- (BOOL)writeAdditionalContent:(id)content toURL:(NSURL *)absoluteURL originalContentsURL:(NSURL *)absoluteOriginalContentsURL error:(NSError **)error

这篇关于我可以访问Core Data中用于外部二进制存储的文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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