Skip to content

Instantly share code, notes, and snippets.

@cellfusion
Created May 18, 2013 14:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cellfusion/5604605 to your computer and use it in GitHub Desktop.
Save cellfusion/5604605 to your computer and use it in GitHub Desktop.
CoreData 使ってユニークキーもどきを NSManagedObject.valideateForInsert 内で判断してみた
- (BOOL)validateForInsert:(NSError **)error {
NSLog(@"validateForInsert:%@", self.id);
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Hoge"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@", @"id", self.id];
[fetchRequest setPredicate:predicate];
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
if (results.count > 0) {
return NO;
}
return YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment