RestKit Dynamic mapping of map relationship based on value

I am using RestKit to parse JSON and convert it to Core Data NSManagedObjects. Here is an example of JSON.

{ "events": [ { "description": "...", "subject_type": "photo", "subject": { "id": 1, "thumb_url": "...", "medium_url": "...", "large_url": "..." } }, { "description": "...", "subject_type": "user", "subject": { "id": 1, "username": "...", "followers": "..." } } ] } 

Using RKObjectMappingProvider and RKManagedObjectMapping , I map the "events" array to separate Core Data Event objects. It works great.

Event now has two User and Photo relationships on it. Now I need to map the array of objects to the corresponding Core Data object based on the value of "subject_type" and set it to the correct ratio on Event .

I tried using RKDynamicObjectMapping , but I do not know how to specify this for a "dynamic relationship". I need to somehow set the name of the destination relationship based on the value of subject_type .

Any thoughts?

+7
source share
3 answers

In RestKit, RKDynamicMapping also allows you to use the specified block to perform more complex dynamic mapping operations, such as a description. The specific method that gives this feature is called setObjectMappingForRepresentationBlock

Here are some docs on this method: http://restkit.org/api/latest/Classes/RKDynamicMapping.html#//api/name/setObjectMappingForRepresentationBlock :

The RestKit unit tests illustrate some simple cases for this method.

+1
source

It seems that you should use RKDynamicObjectMapping (see Object Mapping ). You can create user mapping and photo mapping, and then use RKDynamicObjectMapping as follows:

 [dynamicMapping setObjectMapping:userMapping whenValueOfKeyPath:@"subject_type" isEqualTo:@"user"]; [dynamicMapping setObjectMapping:photoMapping whenValueOfKeyPath:@"subject_type" isEqualTo:@"photo"]; 
0
source

I recently ran into this problem. Tracing through RestKit looks like an attempt to apply all object mappings to all relationships in an RKDynamicMapping instance. See applyRelationshipMappings in RKMappingOperation.m

I came up with a solution that is a hack, but did not require me to change the backup code much.

In RKMappingOperation.m, I changed the following method:

 destinationObjectForMappingRepresentation:parentRepresentation:withMapping:inRelationship: 

I added the following code (starting with a comment) that checks to see if the assignment of the relationship is the same type of object that is applied, and only continues if it matches. (This has not been rigorously tested, but works in my particular use case.)

 - (id)destinationObjectForMappingRepresentation:(id)representation parentRepresentation:(id)parentRepresentation withMapping:(RKMapping *)mapping inRelationship:(RKRelationshipMapping *)relationshipMapping { RKObjectMapping *concreteMapping = nil; if ([mapping isKindOfClass:[RKDynamicMapping class]]) { concreteMapping = [(RKDynamicMapping *)mapping objectMappingForRepresentation:representation]; if (! concreteMapping) { RKLogDebug(@"Unable to determine concrete object mapping from dynamic mapping %@ with which to map object representation: %@", mapping, representation); return nil; } // Make sure the destination of a core data relationship is the right entity class for the object we're mapping; if ([self.destinationObject respondsToSelector:@selector(managedObjectContext)]) { NSEntityDescription *destinationEntity = [self.destinationObject entity]; NSDictionary *destinationPropertiesDictionary = [destinationEntity propertiesByName]; NSRelationshipDescription *destinationPropertyForDestinationKeyPath = [destinationPropertiesDictionary valueForKey:relationshipMapping.destinationKeyPath]; NSString *relationshipDestinationClassName = [[destinationPropertyForDestinationKeyPath destinationEntity] name]; NSString *mappedObjectClassName = [NSString stringWithCString:class_getName(concreteMapping.objectClass) encoding:NSUTF8StringEncoding]; if (![relationshipDestinationClassName isEqualToString:mappedObjectClassName]) { return nil; } } 

...

0
source

All Articles