Warm tip: This article is reproduced from stackoverflow.com, please click
cocoa-touch iphone objective-c xcode

Core Data unique attributes

发布于 2020-04-15 10:41:11

Is it possible to make a Core Data attribute unique, i.e. no two MyEntity objects can have the same myAttribute?

I know how to enforce this programatically, but I'm hoping there's a way to do it using the graphical Data Model editor in xcode.

I'm using the iPhone 3.1.2 SDK.

Questioner
robinjam
Viewed
14
robinjam 2010-02-12 00:05

I've decided to use the validate<key>:error: method to check if there is already a Managed Object with the specific value of <key>. An error is raised if this is the case.

For example:

- (BOOL)validateMyAttribute:(id *)value error:(NSError **)error {
    // Return NO if there is already an object with a myAtribute of value
}

Thanks to Martin Cote for his input.