Question

I have two NSManagedObject named Event and Image. 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.

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. 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. 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.

Thanks a lot.

Was it helpful?

Solution

Deleting objects with coredata is pretty quick. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top