Skip to content

Instantly share code, notes, and snippets.

@robb
Created August 23, 2013 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robb/6317785 to your computer and use it in GitHub Desktop.
Save robb/6317785 to your computer and use it in GitHub Desktop.
// MTLManagedObjectAdapter.m
- (NSPredicate *)uniquingPredicateForModel:(MTLModel<MTLManagedObjectSerializing> *)model {
NSSet *propertyKeys = nil;
if ([self.modelClass respondsToSelector:@selector(propertyKeysForManagedObjectUniquing)]) {
propertyKeys = [self.modelClass propertyKeysForManagedObjectUniquing];
}
NSPredicate *predicate = nil;
for (NSString *propertyKey in propertyKeys) {
NSString *managedObjectKey = [self managedObjectKeyForKey:propertyKey];
if (managedObjectKey) {
id transformedValue = [model valueForKeyPath:propertyKey];
NSValueTransformer *attributeTransformer = [self entityAttributeTransformerForKey:propertyKey];
if (attributeTransformer != nil) transformedValue = [attributeTransformer transformedValue:transformedValue];
NSPredicate *subpredicate = [NSPredicate predicateWithFormat:@"%K == %@", managedObjectKey, transformedValue];
predicate = [NSCompoundPredicate andPredicateWithSubpredicates:(predicate ? @[predicate, subpredicate] : @[subpredicate])];
}
}
return predicate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment