Basic data. How to check attribute uniqueness within

I would like to know how to implement validation in Core Data. What I would like to do is make sure that the attribute is unique within the scope of the associated parent. In other words, I'm wondering how to implement the validates_uniqueness_of :field, :scope => :parent ,: validates_uniqueness_of :field, :scope => :parent (from rails / activerecord) paradigm in Core Data.

For example, suppose I create two models โ€” one for Blog and one for Post. Each column has a title attribute. Different blog objects may have posts with the same headings, but how can I check the uniqueness of the title within the blogs?

Thanks!

+1
source share
1 answer

Communicate with the parent and take a set of messages. You can then run the predicate against it to check for uniqueness, for example:

 NSSet *set = [[self parent] posts]; NSSet *filtered = [set filteredSetWithPredicate:[NSPredicate preicateWithFormat:@"self != %@ and title == %@", self, [self title]]]; if ([filtered count] > 0) return NO; return YES; 
0
source

All Articles