简体   繁体   English

通过避免Zombie对象的新属性替换核心数据属性

[英]Replacing a Core Data property by a new one avoiding Zombie objects

I have two NSManagedObject named Event and Image. 我有两个名为事件和图像的NSManagedObject。 The Event class has a property (relationship with a inverse relationship also) named images, which is composed of a NSSet containing objects of the class Image. Event类具有一个名为image的属性(也具有逆向关系),该属性由包含Image类的对象的NSSet组成。

When updating the Events from the server I want just to replace the old Image NSSet by a new one, which also could contain previously existing Image objects or new ones. 从服务器更新事件时,我只想用新的Image NSSet替换旧的Image NSSet,它也可以包含以前存在的Image对象或新的Image对象。 What I do then is something like: 我要做的是:

event.images = [NSSet setWithArray:imagesParser_.imagesArray];

This fulfills my purpose, but my worry is that still some Image objects belonging to the old NSSet, now without any link to any Event, could remain forever in the database. 这实现了我的目的,但我担心的是,仍然属于旧NSSet的某些Image对象(现在没有任何Event的任何链接)可能会永久保留在数据库中。 I thought that maybe doing a clean (looking in the DB for Image objects without Event and removing them from DB) after the update would fix this, but I do not know how efficient it is. 我以为在更新后进行干净的清理(在DB中查找没有Event的Image对象并将其从DB中删除)可以解决此问题,但是我不知道它的效率如何。

Thanks a lot. 非常感谢。

Deleting objects with coredata is pretty quick. 使用coredata删除对象非常快。 I would personally go through and delete all of the objects attached to my event and then add the new ones. 我将亲自检查并删除附加到事件中的所有对象,然后添加新对象。

You could also take your approach and infrequently go through and remove all images without any events. 您也可以采用这种方法,并且不经常查看并删除所有图像,而不会发生任何事件。 This only becomes problematic if, in the future, images can exist without an event (say you create a new type of data that wants photos). 这只会在将来在没有事件的情况下存在图像(例如您创建需要照片的新型数据)时才成为问题。 It really is not time or memory intensive. 确实不是时间或内存密集型。

It is a good idea to keep your database clean, otherwise you could run into complications in the future. 保持数据库干净是一个好主意,否则将来可能会遇到麻烦。 Any effort you put into it now generally benefits you in the future. 现在,您付出的任何努力通常都会使您将来受益。 If you do not need an object anymore delete it somehow. 如果您不再需要某个对象,则以某种方式将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM