Deleting Duplicate Objects in Core Data (iphone)
In my iPhone app I provide users with a view of industry news items. I get this list 开发者_运维知识库as an xml file from my server. Parsing and inserting the xml data into my Core Data repository is a no brainer, but there are a few cases where I might get duplicate news item entries.
I thought a good solution would be to insert all the updates when I process the xml feed and then remove any duplicates. But I can’t figure out how to do the latter. How does one remove duplicate objects in Apple’s Core Data framework?
To make this concrete, let’s say I have a news item:
News Item - uniqueId (set by the external system) - title - newsText
Is there any succinct way to tell Core Data to just delete duplicate objects, where a duplicate object is defined as an object with the same “uniqueId”? I.e., without doing an explicit fetch in my code and making sure not to insert the object if an object witht he same “uniqueId” already exists?
Core Data does not support deleting "duplicates" because the only notion of object identity is the NSManagedObjectID
assigned to each object. Since you can't set this ID manually, you won't be able to use it for uniqueing on insert. You have (at least) two options:
Do a fetch on insert, as you suggest. Testing will reveal whether this is too slow: until you test it however, don't assume that this solution won't work. You could improve the performance by doing all of the insertion into an
NSInMemoryPersistentStore
then migrating this persistent store to a permanent (on-disk) store to save. If you can keep all of the inserted objects in memory, this will be very fast.You can insert all the objects, save, then do a fetch and delete all but one of the objects. Again, this may be more performant than you suspect.
In both cases, the amount of code is minimal. The Core Data instruments in the Instruments.app will be your best tool for judging the performance of both approaches.
If you create an attribute, uniqueId, in Core Data UNIQUE and NOT optional then it won't copy the same item again.
精彩评论