Skip to content

Instantly share code, notes, and snippets.

@blakewatters
Created August 2, 2011 02:16
Show Gist options
  • Save blakewatters/1119459 to your computer and use it in GitHub Desktop.
Save blakewatters/1119459 to your computer and use it in GitHub Desktop.
Workaround for Managed Object Relationship Sequencing Problems
// Add `NSMutableDictionary* _managedObjectMappingsAndObjects;` to ivars and dealloc
- (void)objectMapper:(RKObjectMapper*)objectMapper didMapFromObject:(id)sourceObject toObject:(id)destinationObject atKeyPath:(NSString*)keyPath usingMapping:(RKObjectMapping*)objectMapping {
if ([objectMapping isKindOfClass:[RKManagedObjectMapping class]]) {
if (nil == _managedObjectMappingsAndObjects) {
_managedObjectMappingsAndObjects = [NSMutableDictionary new];
}
if (nil == [_managedObjectMappingsAndObjects objectForKey:objectMapping]) {
[_managedObjectMappingsAndObjects setObject:[NSMutableArray arrayWithObject:destinationObject] forKey:objectMapping];
} else {
[[_managedObjectMappingsAndObjects objectForKey:objectMapping] addObject:destinationObject];
}
}
}
- (void)objectMapperDidFinishMapping:(RKObjectMapper*)objectMapper {
for (RKManagedObjectMapping* objectMapping in _managedObjectMappingsAndObjects) {
for (NSManagedObject* object in [_managedObjectMappingsAndObjects objectForKey:objectMapping]) {
NSDictionary* relationshipsAndPrimaryKeyAttributes = [objectMapping relationshipsAndPrimaryKeyAttributes];
for (NSString* relationshipName in relationshipsAndPrimaryKeyAttributes) {
NSString* primaryKeyAttribute = [relationshipsAndPrimaryKeyAttributes objectForKey:relationshipName];
RKObjectRelationshipMapping* relationshipMapping = [self.objectMapping mappingForKeyPath:relationshipName];
id<RKObjectMappingDefinition> mapping = relationshipMapping.mapping;
if (! [mapping isKindOfClass:[RKObjectMapping class]]) {
RKLogWarning(@"Can only connect relationships for RKObjectMapping relationships. Found %@: Skipping...", NSStringFromClass([mapping class]));
continue;
}
RKObjectMapping* objectMapping = (RKObjectMapping*)mapping;
NSAssert(relationshipMapping, @"Unable to find relationship mapping '%@' to connect by primaryKey", relationshipName);
NSAssert([relationshipMapping isKindOfClass:[RKObjectRelationshipMapping class]], @"Expected mapping for %@ to be a relationship mapping", relationshipName);
NSAssert([relationshipMapping.mapping isKindOfClass:[RKManagedObjectMapping class]], @"Can only connect RKManagedObjectMapping relationships");
NSString* primaryKeyAttributeOfRelatedObject = [(RKManagedObjectMapping*)objectMapping primaryKeyAttribute];
NSAssert(primaryKeyAttributeOfRelatedObject, @"Cannot connect relationship: mapping for %@ has no primary key attribute specified", NSStringFromClass(objectMapping.objectClass));
id valueOfLocalPrimaryKeyAttribute = [object valueForKey:primaryKeyAttribute];
if (valueOfLocalPrimaryKeyAttribute) {
id relatedObject = [objectMapping.objectClass findFirstByAttribute:primaryKeyAttributeOfRelatedObject withValue:valueOfLocalPrimaryKeyAttribute];
[object setValue:relatedObject forKey:relationshipName];
}
}
}
}
}
@buzali
Copy link

buzali commented Aug 2, 2011

I think there's an error in this code, what's destinationObject on line 25/28?

@blakewatters
Copy link
Author

Buzali -

Thanks. I have updated it to reference self.targetObject instead.

@blakewatters
Copy link
Author

@buzali
Copy link

buzali commented Aug 2, 2011

I can't get it to work.
I think in line 10 and 13, it should be objectMapping instead of self.objectMapping. Also self.targetObject is always nil. Is targetObject supposed to be within the ObjectLoader?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment