Created
August 2, 2011 02:16
-
-
Save blakewatters/1119459 to your computer and use it in GitHub Desktop.
Workaround for Managed Object Relationship Sequencing Problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; | |
} | |
} | |
} | |
} | |
} |
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
Added to RestKit wiki @ https://github.com/RestKit/RestKit/wiki/Sequencing-issue-with-core-data-relationships