CoreData - Is there a good way to increase the number of elements?

Just wondering if there is a good way for upsert elements in CoreData db ?

Or is there a way to view CoreData db as a collection?

I mean, if I insert an element into db, and if there is already an identical redundant element there, db ignores it. Any way to do this? or should I query every time I insert to avoid redundancy?

+4
source share
2 answers

No. There is no way for Core Data to find out how you consider an element to be "identical" or "redundant." The definitions of these words can change with almost every entity you create - for example, departments in a business can have unique names, but several people can have the same name (and often do this).

However, you can take advantage of the power of Core Data queries and run a quick query using NSPredicate to find out if there is a record with your chosen identifier. You can expose this request to your own method (possibly in a subclass of the managed object) so that it can be conveniently used.

+4
source

I read this article recently

https://www.upbeat.it/upsert-in-core-data/

Basically, first create a name constraint, then specify TrumpMergePolicy in the context of Core Data.

 managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy 
+2
source

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


All Articles