views:

36

answers:

3

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?

A: 

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.

Shaggy Frog
The question does state that there is a timestamp which is considered as a "unique record", although not necessarily implemented as a "primary key"
Mark
+1  A: 

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.

Marcus S. Zarra
yes, but my question states that looping over them and doing a check for each one is simply not fast enough, oh well, looks like this might be the only way... BTW its not that im using the timestamp as a key, its more of a business rule that states that any record on the same timestamp are considered duplicates
Mark
I would be surprised if the fetching and uniquing was more than 5% of the total time of your parsing considering how slow XML parsing is. I would suggest profiling the code and seeing where your hot spots are. Fetching and testing for uniqueness is the only way it can be done although as I mentioned they can be prefetched to improve performance.
Marcus S. Zarra
Thanks Marcus, turns out that (so far) you are right, the time taken is minimal, so thats what ill do... thanks
Mark
A: 
tc.
I know timestamps are not unique, Im not claiming they are, Im saying that my business rules state that objects/records at the same time are considered equal
Mark