Unique CoreData (IOS) constant for multiple columns?

Is it possible in CoreData for an iPhone application to have a unique constant in multiple columns?

For instance:

  • have Event, EventItems, Items
  • EventItems object has an ORDER column
  • therefore, the ORDER column for EventItem must be unique for all instances belonging to the same event

So the questions are:

  • How do I set this restriction in coredata?
  • If it does not support any suggestions, how to create programmatically?
+6
source share
3 answers

Make

For any master data restriction that runs on more than one managed entity at once, you want to look at an implementation:

- (BOOL)validateForDelete:(NSError **)error - (BOOL)validateForInsert:(NSError **)error - (BOOL)validateForUpdate:(NSError **)error 

(I usually have kernel data, make .h and .m files for an object, and then create my own category for such things, so I don’t have much work if I changed the object a bit)

If you have something you just need to make sure the values ​​in one managed entity are correct, you can use -validate<Key>:error:

To do what you are looking for, I would do validityForInsert / validateForUpdate EventItems call a generic method (possibly validateUniqueOrder). In this method, I would use the relation of EventItems to the event, and then retrieve all the EventItems associated with the event, and then check for uniqueness. I have rather small sets of relations, so I didn’t worry about anything, but if you have a lot of event elements related to the given events, you can look at the setPropertiesToFetch NSFetchRequests method. Or maybe you can find a query that can directly look for duplicate values ​​(I never could, so if you do, answer here to enlighten me).

+6
source

How do I configure this restriction in coredata?

You control what is included in the data warehouse, so you can impose any restrictions that you like, no matter how difficult they are. But Core Data is not a database and does not implement the kinds of automatic restrictions that you usually find in a DBMS.

If it does not support any suggestions, how to use it programmatically?

I would do a check at the point in your code where you create or modify the affected object. In your case, you can create a custom parameter for the OrderItem 'order' property, which compares the proposed "order" with the settings of all other EventItems associated with the same event. Or you can put a check in Event and use the appropriate accessory to check for any new EventItems as they are added.

+3
source

Unique constraints ensure that Entity entries are unique across specified fields. But the unique constraints along with the To-Many attitude lead to many weird problems in conflict resolution.

eg. "Hanging link to an invalid object."

This post is mainly focused on a small issue that can take several days.

http://muhammadzahidimran.com/2016/12/08/coredata-unique-constraints-and-to-many-relationship/

0
source

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


All Articles