How to avoid duplicate entries?

I have a web service call that returns XML, which I convert to domain objects. Then I want to insert these domain objects into the Core Data repository.

However, I really want to make sure that I do not insert duplicates (objects have a date stamp that makes them unique, which I hope to use to verify uniqueness). I really do not want to go in cycles on each object, to make selection, and then to interpose if nothing is found, as this would be very poor in performance ...

I am wondering if there is an easier way to do this? Perhaps a “group by” on my objects in mind ???? Is it possible?

+3
source share
3 answers

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

Since you delete one value, you can get all the relevant objects at once by setting the predicate:

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

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

Also a word of advice. Timestamp is a terribly unique identifier. I would highly recommend you review this.

+5
source

Timestamps are not unique. However, we will assume that you have unique identifiers (e.g. UUID / GUID / whatever).

SQL- GUID , ( , ) ( , , , ...). , :— , , .

+1

, ? - ? ( .) - Entity , , .

0

Source: https://habr.com/ru/post/1760646/


All Articles