Consider the following correlation between dictionary objects in a feed and sorted entities in core data:
Feed CoreData
---- --------
A A
B B
C C
D D
As I'm enumerating the feed, I check if A's [stringForKey:@"name"]
isEqualTo A.name
in the entity. If it's a match, I update the entity. If it's not, I insert a new entity into CoreData.
This works fine for updating and inserting, but not for deletion. Consider that object C is removed from the feed:
Feed CoreData
---- --------
A A
B B
D C
D
When I get to "D" in the feed, it will see that object "C" in CoreData is not a match and create a new object D. So I have two problems now: I have two "D" objects, and object "C" does not get removed from CoreData.
So while I want to end up with this:
Feed CoreData
---- --------
A A
B B
D D
What I currently get is this:
Feed CoreData
---- --------
A A
B B
D C
D
D
This must be a common issue so I'm wondering what the best practice is here for determining when to remove entities from Core Data.
This is what I do when I loop through items to determine whether to update, insert, or delete: