Using Core Data to Save Just 'Favorite' Objects

Hi all,

First of all, I love the book, and it’s been a big help, but I’ve come across a use case for Core Data which I can’t seem to work out the best-practice implementation for, and would appreciate some advice on it:

My app systematically generates a number of NSManagedObjects and presents a list of them to the user, who then has the option to mark any of these as ‘favorites’, thereby saving them for later. The rest are good to be forgotten when the app closes.

Using a child context to store the full set of ‘short-term’ objects seems like the way to go, but how would I go about saving just the user-selected ones from the child context back to the parent? I know that generally when the child context saves, all of its content shifts up to the parent. Is there a way to move specific objects from one context to another that wouldn’t be too much of a hack?

Any advice is appreciated. Perhaps someone else has experienced the same need?


I think you could just delete the objects that you don’t want saved, before doing the save back to the parent context. Something like


for each of the unwanted objects.

Hi @sgerrard,

Thanks for the quick reply!

I considered it, but the problem is - when does one delete all of the others? The list of suggestions is designed to stay on-screen until the app’s closed, and when a user makes one a favorite, it’s not to say they won’t want to do the same with others, so they need to stay around.

I suppose potentially it could be done using a marker property for the favorites, bulk deleting all others, then saving the remainder back to the parent, all from a method triggered by the AppDelegate’s terminate method, but it seems a little inelegant, and it’d be safer to save favorites as they are selected, rather than on termination.

Did you have something else in mind?


Thanks for the advice.

Hi @officially4h. Did you mean mine? If so, I was speculating that it would be an elegant solution. I would need some more input regarding the suggestion from @sgerrard before I would know how it could be implemented, at least for my scenario.

If you want to keep the list available all the time, and save favorites when they get selected, I think I would make the list not be managed objects in a context, but just an ordinary array of objects. Then when one is selected, create the managed object from it and save that.

What is the benefit of having the candidate list in a managed context?

Thanks for the suggestion. That’s actually how I have it currently. It’s not so much about any benefit of having the suggestions managed, so much as it seems foolish to have 2 classes: one the subclass of NSManagedObject for the favorites, the other a custom class for the suggestions, both with the same properties defined twice over. Plus this means having to copy all the properties of one into the other when a suggestion becomes a favorite.

Would that still be the best way to do it?

Another possible alternative I thought of would be to have them all as managed objects, and just create a new instance in the ‘favorites’ context when a favorite is selected, copying the properties from the original suggestion object at that point. Still seems pretty hacky to me though.


This topic was automatically closed after 166 days. New replies are no longer allowed.