views:

437

answers:

3

I have an iPhone project where I have a series of Managed Objects in a Managed Object Context within the main application thread. On a separate thread, I import new objects from a webserver into a second Managed Object Context.

If the import goes ok without errors, I save the import context. This triggers contextDidSave from which I call mergeChangesFromContextDidSaveNotification. The two contexts merge ok. So far, so good.

My desired functionality is to have any objects that are in the original MOC but not in the imported MOC to be deleted (the idea is that the user is refreshing the data, and so old data should be deleted), but the merge seems to just combine the 2 MOCs.

Please can you advise if there is a way of managing the merge of the 2 MOCs so that those not in one are deleted in the other?

Thanks,

John

+1  A: 

When you create the new moc it has access to all of the old objects so you can handle the deletion on the background thread as part of the import.

Marcus S. Zarra
Thanks - I ended up going through all the objects in the moc and seeing if they were in the collection of objects that were to be imported. If they weren't, I deleted them in the import moc. Thus when the mocs were merged, the objects that I didn't want were deleted.
A: 

What you describe is not really just importing. The logic of deciding what objects are being "updated" is application specific. You can handle that on the import thread by fetching those objects and updating them rather than creating new instances or you can do the work after meting. Of the two options, I would prefer the first one as it's more inline with the Core Data way of things.

Barry Wark
A: 

What I do in such situation, is a method "prepareForDelet" that marks a property "delete" to YES. Then when I import my objects, if one exists already, I mark as delete NO. When my thread is over, I can delete every objects whose delete is YES

Yann Bizeul