I see two ways to do this. It seems to me that the latter (your proposed method) is the best solution.
I changed id to primaryKey, because I don’t think it would be nice to use id as the name of a variable or method in Object-C, since this is a keyword. I could work, I never tried. I also suggested that primaryKey is an NSNumber, as this will be stored in Core Data.
One :
for (id data in someSetOfDataToImport) {
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Story" inManagedObjectContext:context]];
[request setPredicate:[NSPredicate predicateWithFormat:@"primaryKey = %d", primaryKey]];
NSUInteger count = [context countForFetchRequest:request error:nil];
[request release];
if (count > 0)
continue;
}
2 , , :
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Story" inManagedObjectContext:context]];
NSArray * allStories = [context countForFetchRequest:request error:nil];
[request release];
NSMutableArray * allPrimaryKeys = [[allStories valueForKeyPath:@"@distinctUnionOfObjects.primaryKey"] mutableCopy];
for (id data in someSetOfDataToImport) {
if ([allPrimaryKeys containsObject:data.primaryKey])
continue;
[allPrimaryKeys addObject:data.primaryKey];
}
[allPrimaryKeys release];