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

2
  • so why not update or fetch the items to be deleted and delete only them? Commented Apr 5, 2016 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. Commented Apr 5, 2016 at 19:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that seems to be the way to go. I already found a way to copy an entity here: stackoverflow.com/questions/9344064/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.