Core Data uniqueness

2019-08-15 06:48发布

问题:

Is there any way I can validate a value updated in a Core Data entity's property against values of the property in other entities in the collection?

At the moment I create an entity with some default values, add it to arrangedObjects, then get the user to modify the various property values. However, I would like to check a particular property and make sure there're no other entities in the array with the same value for that property. What's the best way to do this?

Many thanks, Dany.

回答1:

Manually checking is only a few lines of code with a fast enumeration loop:

BOOL unique = YES;
for (NSManagedObject *obj in collection) {
    if (obj.property == value) {
        unique = NO;
        break;
    }
}