我使用这种方法,但不正确。
- (BOOL)checkExistByEntityName:(NSString *)entityName primaryKeyName:(NSString *)keyName primaryKey:(NSNumber *)primaryKey
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@==%@", keyName, primaryKey];
[request setEntity:entity];
[request setPredicate:predicate];
NSError *error = nil;
NSInteger count = [managedObjectContext countForFetchRequest:request error:&error];
[request release];
if (count > 0) {
return YES;
} else {
return NO;
}
}