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

查看:14
本文介绍了我可以访问 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 继承子类并覆盖处理读取/写入额外"文件的方法.这将使您可以访问文件本身.你可以挂在那里做任何你想做的事情,包括获取 CoreData 自动创建的文件的实际 URL.

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天全站免登陆