Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add unique constraints for some fields in Core Data

Tags:

ios

core-data

I use Xcode for iOS development. I have some entity (for example, User), and I need to set unique constraint for his name, but I can't find how I can do it through visual editor. Is it possible to do it through GUI? Or it's possible through code only? I will be glad to get some screenshot.

like image 678
malcoauri Avatar asked Jan 15 '14 06:01

malcoauri


People also ask

How do you make a core data attribute unique with constraints?

If you look in the Data Model inspector you'll see a field marked "Constraints" – click the + button at the bottom of that field. A new row will appear saying "comma,separated,properties". Click on that, hit Enter to make it editable, then type "sha" and hit Enter again. Make sure you press Cmd+S to save your changes!

What is constraint in Core data?

Save. Unique Constraints are a way to declare a custom attribute to be unique across all instances of an entity. Its intended use case is the import of external data that should be merged with existing objects in the database.

What is Nsmanagedobjectcontext?

An object space to manipulate and track changes to managed objects.


2 Answers

There's a new section in the sidebar when selecting an entity in the editor for Core Data. You can set what constraint(s) you want to be unique across all instances of an entity

For automatic conflict resolution during saves, you'll need to make sure you've got a merge policy set for your managed object context or else you'll just get errors when saving (which might actually be what you want)

[managedObjectContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy]; 

The "Swift version" is exactly the same

managedObjectContext.mergePolicy = .mergeByPropertyObjectTrumpMergePolicyType 

Keep in mind conflict resolution only happens during saves, and not inserts. So if you're making use of a NSFetchedResultsController you will see entities with non-unique constraints as they're inserted.

enter image description here

If you want to sure you have no entities with non-unique constraints in your managed object context without saving (if you're making use of a FRC), this answer is still probably the best way to go. Although, keep in mind, it's expensive if you're doing a lot of inserts, since NSFetchRequests are expensive operations.

Sample code for this demo can be found here

like image 89
Zachary Orr Avatar answered Sep 23 '22 02:09

Zachary Orr


Swift solution:

As noted in the other answer, you can have unique constraints in Core Data for iOS9 onwards.

To do this, first add constraints to the Entity from the Core Data Editor (explaination in Zachary's answer).

Then add this line in code:

managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy 

Note: The uniqueness is checked when you do managedObjectContext.save() not while you're just adding objects to the managed object.

NSMergeByPropertyObjectTrumpMergePolicy is just one of the merge policies, which overwrites the old value with the new one. You might want to check the documentation for other options.

like image 30
Nitin Nain Avatar answered Sep 23 '22 02:09

Nitin Nain