如何在删除Core Data对象时处理外部数据的清除 [英] How to handle cleanup of external data when deleting Core Data objects

查看:85
本文介绍了如何在删除Core Data对象时处理外部数据的清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的数据模型包括图像,我保持那些外部的数据库并简单地存储到图像的路径/ URL。 (如Apple的Core Data演示文稿中的推荐)



删除我的图像对象时,我可以手动遍历关系并删除图像文件,但我想知道是否有更多优美的方式来做到这一点。



理想的解决方案将绑定到图像对象某种方式,并将使用Core Data撤消/重做。

解决方案

在你的image实体类中,实现 willSave 。检查 [self isDeleted] 并删除该文件,如果是这样。这推迟实际删除文件,直到商店保存,这给你一些撤消能力。



[eta .: Phil Calvin的下面的评论是正确的 - didSave 可能是更好的地方,如果你使用多个上下文。]



[eta。更多:] MartinW提出了一个很好的点 - 如果对象从未保存,将/不保存将不会被调用。删除未保存的对象只是从上下文中将其删除,并将其丢弃,而没有特殊的生命周期事件。只是为了更复杂的事情,你可以撤消和重做一个删除,包括(我认为)这种。





这可能是覆盖 prepareForDeletion:,加上 awakeFromSnapshotEvents:以捕获删除和重新删除。要支持撤消/重做,您需要不要在现场简单地删除文件,而是使用某种要删除的注册表(例如,在发布保存通知时,共享的可变文件名集会清除)。



或者,如果您可以使用BLOB字段而不是文件,则可以选中允许外部存储框二进制属性,将jpeg数据放在那里,并且获得一些(不是全部)文件存储的优点,没有(大部分)头痛。小二进制将保留在db中;大于私人阈值的任何内容将被分流到一个单独的隐藏的核心数据管理文件中。核心数据仍然必须加载整个东西到一个NSData当对象中出现故障,虽然。我使用这种方法为用户头像类型的小图像。



最后,如果没有其他内容保存在images目录中,您可以注册didSave通知,并在任何保存后手动清理。对所有Image.filename属性运行获取请求,将其与相关映像dir的目录列表进行比较,根据需要删除。我在开发过程中使用这种方法作为我的大棒,以确保一切都在做它应该做的事。



[让我知道这些方法的成功或困难,我将保持此更新。]


I am fairly new to Core Data and have run into an issue which others must have encountered.

My data model includes images and I am keeping those external to the database and simply storing a path/URL to the image. (as recommended in one of Apple's Core Data presentations)

When deleting my image object I can manually traverse relationships and remove the image files but I am wondering if there is a more elegant way to do this.

The ideal solution would be tied to the image object somehow and would work with Core Data undo/redo.

解决方案

In your "image" entity class, implement willSave. Check [self isDeleted] and delete the file if so. This postpones actual deletion of the file until the store is saved, which gives you some undo-ability. Set up appropriate cascade rules to delete the image entities when their owner goes away, and there you go.

[eta.: Phil Calvin's comment below is right - didSave is probably a better place, if you're using multiple contexts.]

[eta. much later:] MartinW raises an excellent point - if the object has never been saved, will/did save won't get called. Deleting an un-saved object just un-inserts it from the context and throws it away, with no special lifecycle events. And just to complicate matters more, you can "undo" and "redo" a deletion, including (I think) this kind.

A few approaches that come to mind:

This might be a case for overriding prepareForDeletion:, plus awakeFromSnapshotEvents: to catch un-deletion and re-deletion. To support undo/redo, you'll need to not simply delete the file on the spot, but use some kind of "to be removed" registry (e.g. a shared mutable set of filenames to clean up when a save notification is posted). Then will/didSave are out of the picture.

Or, if you can live with BLOB fields instead of files, you could check the "allows external storage" box on a binary property, put the jpeg data there, and get some (not all) of the advantages of file storage without (most of) the headaches. Little binary will be kept in the db; anything bigger than a private threshold will be shunted out into a separate hidden core-data-managed file. Core data still has to load the whole thing into an NSData when faulting in the object, though. I use this approach for "user avatar"-type small images.

Finally, if nothing else is kept in the images directory, you could register for didSave notifications and manually clean up after any save. Run a fetch request for all the Image.filename properties, compare it to a directory listing of the images dir in question, delete as appropriate. I use this approach as my "big stick" during development to make sure everything else is doing what it should.

[Let me know of successes or hardships with these approaches and I'll keep this up to date.]

这篇关于如何在删除Core Data对象时处理外部数据的清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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