Mark
Mark

Reputation: 14930

How can you avoid inserting duplicate records?

I have a web service call that returns XML which I convert into domain objects, I then want to insert these domain objects into my Core Data store.

However, I really want to make sure that I dont insert duplicates (the objects have a date stamp which makes them unique which I would hope to use for the uniqueness check). I really dont want to loop over each object, do a fetch, then insert if nothing is found as that would be really poor on performance...

I am wondering if there is an easier way of doing it? Perhaps a "group by" on my objects in memory???? Is that possible?

Upvotes: 3

Views: 3349

Answers (3)

tc.
tc.

Reputation: 33592

Timestamps are not unique. However, we'll assume that you have unique IDs (e.g. a UUID/GUID/whatever).

In normal SQL-land, you'd add an index on the GUID and search, or add a uniqueness constraint and then just attempt the insert (and if the insert fails, do an update), or do an update (and if the update fails, do an insert, and if the insert fails, do another update...). Note that the default transactions in many databases won't work here — they lock rows, but you can't lock rows that don't exist yet.

Upvotes: 1

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

Your question already has the answer. You need to loop over them, look for them, if they exist update; otherwise insert. There is no other way.

Since you are uniquing off of a single value you can fetch all of the relevant objects at once by setting the predicate:

[myFetchRequest setPredicate:[NSPredicate predicateWithFormat:@"timestamp in %@", myArrayOfIncomingTimestamps]];

This will give you all of the objects that already exist in a faulted state. You can then run an in memory predicate against that array to retrieve the existing objects to update them.

Also, a word of advice. A timestamp is a terribly uniqueID. I would highly recommend that you reconsider that.

Upvotes: 5

Shaggy Frog
Shaggy Frog

Reputation: 27601

How do you know a record would be a duplicate? Do you have a primary key or some other unique key? (You should.) Check for that key -- if it already exists in an Entity in the store, then update it, else, insert it.

Upvotes: 0

Related Questions