0

My iOS app downloads record from a 3rd party database, and stores them locally using CoreData. The user is able to edit some aspects of the stored records, eg user notes can be added in addition to notes from the database.

Occasionally, the database gets updated, and I provide a refresh function. Instead of checking what parts of the entries are different, I just brute-force remove the old one and replace it with a new one.

However, this also removes the user notes. I tried saving them before the refresh, and re-adding them after inserting the new entry, but once the original entry gets deleted, the user note is also deleted because of the "Cascade" delete rule. If I set the delete rule to "No Action" for notes, then all notes will not be deleted.

So I was thinking, is it possible to temporarily change the delete rule of the user note while updating so that it doesn't get deleted with the old entry?

Or maybe my approach is completely wrong, and there are better ways to handle this?

UPDATE: I have created a follow up question here: Change relationship of NSManagedObject to different context

Community
  • 1
  • 1
koen
  • 5,383
  • 7
  • 50
  • 89
  • so why not update or fetch the items to be deleted and delete only them? – Wain Apr 05 '16 at 19:44
  • I'm still thinking about the correct approach. What I still don't understand is when I change the relationship for a user note from the old to the new entry, and then delete the old entry, the user note is gone as well. Thinking out loud when I type this (and away from my Mac), I probably need to save the context after changing the relationship, then delete the old entry, and then save again. – koen Apr 05 '16 at 19:49

1 Answers1

0

You are not allowed to change model after it was instantiated, except versioning. The way I think, you should create new entity, say, CustomNote and store some unique identifier to original "record". Then just retrieve this notes by id. Although, it may be some more advanced approach with relationships, this is the simplest.

kirander
  • 2,202
  • 20
  • 18
  • Yes, that seems to be the way to go. I already found a way to copy an entity here: http://stackoverflow.com/questions/9344064/coredata-duplicate-existing-object – koen Apr 03 '16 at 18:58