RestKit and Restlet JSON key-value - ios

In RestKit for mapping a class to JSON key-value we use,
RKObjectMapping * userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping mapKeyPath:#"PrimaryKey" toAttribute:#"id"];
[[RKObjectManager sharedManager].mappingProvider setMapping:usereMapping forKeyPath:#"clientUser"];
However in android, if restlet is used, we just have to add #JsonProperty("PrimaryKey"), when declaring the variable in the class. And the Key value mapping is done.
Is there a simpler way for iOS restkit similar to android restlet?
Thanks in advance.

You can do it like this in ios
RKObjectMapping* articleMapping = [RKObjectMapping mappingForClass:[Article class]];
[articleMapping addAttributeMappingsFromDictionary:#{
#"title": #"title",
#"body": #"body",
#"author": #"author",
#"publication_date": #"publicationDate"
}];
And
// Create our new Author mapping
RKObjectMapping* authorMapping = [RKObjectMapping mappingForClass:[Author class] ];
// NOTE: When your source and destination key paths are symmetrical, you can use addAttributesFromArray: as a shortcut instead of addAttributesFromDictionary:
[authorMapping addAttributeMappingsFromArray:#[ #"name", #"email" ]];
REFER HERE

Related

Identify the order of elements persisted in CoreData - ios - objective c

I am creating a response descriptor for json to core data mapping in RestkitManager. The parent object is "level" and it has an array of "sublevel" objects.
RKDynamicMapping *levelMapping = [Level map];
RKResponseDescriptor* levelRd = [RKResponseDescriptor responseDescriptorWithMapping:levelMapping method:RKRequestMethodGET pathPattern:#"entity/:entityId" keyPath:#"summary.levels" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self.objectManager addResponseDescriptor:levelRd];
In Level class
+ (RKEntityMapping *)mapping {
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([self class]) inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[mapping addAttributeMappingsFromDictionary:#{
#"id" : #"id",
#"name" : #"name",
#"state" : #"state"
}];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"sublevel" toKeyPath:#"sublevelList" withMapping:[Sublevel map]]];
return mapping;
}
In Sublevel
+ (RKEntityMapping *)map {
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([self class]) inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[mapping addAttributeMappingsFromDictionary:#{
#"id" : #"id",
#"staticNode.obj.name" : #"name"
}];
return mapping;
}
When I try to fetch sublevel on object level, I get it in random order. Sometimes 2nd sublevel get printed first. Is there any way to maintain the order?
As I understand, when mapping is done, I don't have any control over what is getting persisted in the database. Hence, I am not able to assign any order number myself. Apart from that, I have explored metadata.routing.parameters but for this I need to pass parameters in the API call itself - which is not desirable.
Any pointers on how to maintain the order would be helpful.

RestKit - Post or Put an entity containing nested entities

I have an iOS app that uses RestKit to sync between my Core Data model and a Rails API.
I have Game and Team entities in my Core Data model. A Game has a to-many relationship to Teams. I am trying to update the 'score' attribute of the Teams, and then I am trying to run the putObject method on my RKObjectManager by sending in the Game. The scores of the teams are not updating on the server.
If I change an attribute of the Game, e.g. the 'state', and then send in the Game with putObject, it works correctly.
Is it possible to update more than one object with putObject given that the object has nested objects inside of it? Or do I need to run putObject on the Team when I update its 'score' attribute?
Here is my mapping code for Games.
Class itemClass = [Game class];
RKEntityMapping *mapping = [RKEntityMapping
mappingForEntityForName:#"Game"
inManagedObjectStore:managedObjectStore];
mapping.identificationAttributes = #[#"gameID"];
NSDictionary *standardDict = #{#"id": #"gameID",
#"created_at": #"createdAt",
#"updated_at": #"updatedAt"};
NSDictionary *gameDict = #{#"league_id": #"leagueID",
#"location_id": #"locationID",
#"state": #"state",
//.... more attributes....
};
[mapping addAttributeMappingsFromDictionary:standardDict];
[mapping addAttributeMappingsFromDictionary:gameDict];
[mapping addConnectionForRelationship:#"league" connectedBy:#{#"leagueID": #"leagueID"}];
[mapping addConnectionForRelationship:#"location" connectedBy:#{#"locationID": #"locationID"}];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
NSString *keyPath = nil;
NSString *itemsPath = #"games/:gameID";
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
method:RKRequestMethodAny
pathPattern:itemsPath
keyPath:keyPath
statusCodes:statusCodes];
[manager addResponseDescriptor:responseDescriptor];
NSString *itemPath = #"game";
RKEntityMapping *requestMapping = [mapping inverseMapping];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:itemClass
rootKeyPath:itemPath
method:RKRequestMethodAny];
[manager addRequestDescriptor:requestDescriptor];
//route for manipulating with existing object
RKRoute *itemRoute = [RKRoute routeWithClass:itemClass pathPattern:#"games/:gameID" method:RKRequestMethodAny];
itemRoute.shouldEscapePath = YES;
[manager.router.routeSet addRoutes:#[itemRoute]];
The mapping for a Team is written basically the exact same way, except a Team has a connection to a Game based on the Game's 'gameID.' So --> [mapping addConnectionForRelationship:#"game" connectedBy:#{#"gameID": #"gameID"}];
You are using foreign key mappings on your response mapping:
[mapping addConnectionForRelationship:#"league" connectedBy:#{#"leagueID": #"leagueID"}];
[mapping addConnectionForRelationship:#"location" connectedBy:#{#"locationID": #"locationID"}];
and these are not reversed when you use inverseMapping (because they don't contain enough information to create the inverse).
So, your requestMapping needs to be explicitly updated to include a relationship mapping to tell RestKit to process the relationship and how to map the relationship contents into the resulting JSON.

Map JSON response that has brackets with RESTKit

I'm doing a GET request with RESTKit, and I need sone help mapping the JSON response.
Here is the response that I need to map:
{"limit_hit":false,"providers":
[{"id":876553,
"name":"Cooper, Bradley N, DDS",
"specialty_groups":["Other Provider"],
"tags":[],
"has_comments":false,
"number_of_comments":0,
"locations":
[{"address":"1234 Rifle Range Road, El Cerrito, CA, 94530",
"providers_at_address_count":1,
"client_product_count":0,
"non_client_product_count":2,
"address_lines":["1234 Rifle Range Road, El Cerrito, CA, 94530"],
"address_id":234578,
"specialty_groups":
[{"specialty_group":"Other Provider"}],
"provider_types":
[{"provider_type":"Other Provider"}]},
{"address":"7501 Mission Rd, Shawnee Mission, KS, 66208",
"providers_at_address_count":2,
"client_product_count":0,
"non_client_product_count":2,
"address_lines":["7654 Main S, El Cerrito, CA, 94530"],
"address_id":654432,
"specialty_groups":
[{"specialty_group":"Other Provider"}],
"provider_types":
[{"provider_type":"Other Provider"}]
}]
}]
}
I want to be able to map both addresses, but I don't know how. All I'm able to do currently is map the id, name, has_comments, and number_of_comments (I'm using the keypath of "providers").
Here is my current mapping provider:
+ (RKMapping *)searchMapping
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[ProviderSearch class]];
[mapping addAttributeMappingsFromDictionary:#{
#"id": #"doctorID",
#"name": #"name",
}];
return mapping;
}
What exactly am I doing wrong, and how do I fix it?
Create another method to return the mapping for locations and then associate that mapping to this original one. Like this:
// ProviderLocation.m
+ (RKObjectMapping *)objectMapping
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[ProviderLocation class]];
[mapping addAttributeMappingsFromDictionary:#{
#"address": #"address",
...
}];
return mapping;
}
Relationship:
+ (RKObjectMapping *)searchMapping
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[ProviderSearch class]];
[mapping addAttributeMappingsFromDictionary:#{
#"id": #"doctorID",
#"name": #"name",
}];
RKObjectMapping *locationsMapping = [ProviderLocation objectMapping];
[mapping addPropertyMapping:
[RKRelationshipMapping relationshipMappingFromKeyPath:#"locations" toKeyPath:#"locations" withMapping:locationsMapping]];
return mapping;
}
Just remember to create a NSArray property in ProviderLocation.h named locations.
i've never used RKObjectMapping before but the "locations" you have there are an array of dictionary objects. so you would need an
NSArray loc = [myJson objectForKey:#"locations"];
for(NSDictionary *dict in loc){
//here each dict obj will have your "address", "providers_at_address_count" and etc... so if you want to access any of them you can call...
NSString *addr = [dict objectForKey:#"address"];
}
now somehow convert that to what you are doing with RXObjectMapping and you are golden =P

RestKit 0.20 nested Object mapping (path to object tree different)

I got a problem with mapping a nested object value.
I got two objects with the following properties:
a)
class Input
#property NSString value;
#property NSString title;
b)
class Profile
#property Input myAwesomeInput;
..so a Profile contains an Input object. When I mapp the objects with RestKit (0.20) I get sth. like this:
{ myAwesomeInput_test:{"value":"xyz","title":"a title"}}
What I wanna achieve is:
{myAwesomeInput_test:"xyz"}
So I don't want to map "Input" but just the Input.value. Is that even possible?
At the moment my code looks like this:
RKObjectMapping* inputMapping = [RKObjectMapping requestMapping];
[inputMapping addAttributeMappingsFromArray:#[#"value"]];
RKRequestDescriptor *reqDescInput = [RKRequestDescriptor requestDescriptorWithMapping:inputMapping objectClass:[Input class] rootKeyPath:nil];
RKObjectMapping* searchProfile = [RKObjectMapping requestMapping];
RKRequestDescriptor *reqDescSearchProfile = [RKRequestDescriptor requestDescriptorWithMapping:searchProfile objectClass:[SearchProfile class] rootKeyPath:nil];
[searchProfile addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:#"myAwesomeInput" toKeyPath:#"myAwesomeInput_test" withMapping:inputMapping]];
EDIT: (solved)
Ok I solved it. Hope it's the way people should do it. You can directly address from within the dictionary.
RKObjectMapping* searchProfile = [RKObjectMapping requestMapping];
[aeSearchProfile addAttributeMappingsFromDictionary:#{
#"myAwesomeInput.value": #"myAwesomeInput_test"
}];
RKRequestDescriptor *reqDescSearchProfile = [RKRequestDescriptor requestDescriptorWithMapping:searchProfile objectClass:[SearchProfile class] rootKeyPath:nil];
Use keypaths rather than multiple mappings.
Try this:
RKObjectMapping* searchProfile = [RKObjectMapping requestMapping];
[searchProfile addAttributeMappingsFromDictionary:#{ #"myAwesomeInput.value" : #"myAwesomeInput_test" }];
RKRequestDescriptor *reqDescSearchProfile = [RKRequestDescriptor requestDescriptorWithMapping:searchProfile objectClass:[SearchProfile class] rootKeyPath:nil];

using RestKit to serialize an object with a subobject [POSTing]

I have the following objects setup:
RKObjectMapping* geoPointMapping = [RKObjectMapping mappingForClass:[CRGeoPoint class]];
geoPointMapping.setDefaultValueForMissingAttributes = YES;
[geoPointMapping mapKeyPathsToAttributes:
#"longitude", #"longitude",
#"latitude", #"latitude",
nil];
[objectManager.mappingProvider registerMapping:geoPointMapping withRootKeyPath:#"geometry"];
RKObjectMapping* criteriaMapping = [RKObjectMapping mappingForClass:[CRCriteria class]];
criteriaMapping.setDefaultValueForMissingAttributes = YES;
[criteriaMapping mapKeyPathsToAttributes:
#"type", #"type",
#"geometry", #"geometry",
#"fromDate", #"fromDate",
#"toDate", #"toDate",
#"radius", #"radius",
nil];
[objectManager.mappingProvider registerMapping:criteriaMapping withRootKeyPath:#"criteria"];
But when I try and send the query (with a geometry object). I keep
getting this error:
error received Error Domain=JKErrorDomain Code=-1 "Unable to serialize
object class CRGeoPoint."
Inbound and outbound mapping information is handled separately by RKObjectMappingProvider. You've configured the inbound mapping in that code (downloading the data from your server). But--as pointed out by Shane Zatezalo on the RestKit group--you also need to add a serialization mapping for RestKit to know how to turn your objects into JSON (or whatever other serialization format you might be using).
Check out the Object Mapping tutorial's section on Object Serialization. And take a look at the source for the mapping provider to reassure yourself that these things are separate.
Solution by OP.
The solution was two-fold. First I had to specify the inverse mapping as Sixten pointed out below, second I had to specify the mapping to use for the CRGeoPoint class on the CRCriteria object. Code below:
RKObjectMapping* geoPointMapping = [RKObjectMapping mappingForClass:[CRGeoPoint class]];
geoPointMapping.setDefaultValueForMissingAttributes = YES;
[objectManager.mappingProvider registerMapping:geoPointMapping withRootKeyPath:#"geometry"];
// Build a serialization mapping by inverting our object mapping. Includes attributes and relationships
RKObjectMapping* geoPointSerializationMapping = [geoPointMapping inverseMapping];
// You can customize the mapping here as necessary -- adding/removing mappings
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:geoPointSerializationMapping forClass:[CRGeoPoint class]];
RKObjectMapping* criteriaMapping = [RKObjectMapping mappingForClass:[CRCriteria class]];
criteriaMapping.setDefaultValueForMissingAttributes = YES;
[criteriaMapping mapKeyPathsToAttributes:
#"type", #"type",
#"fromDate", #"fromDate",
#"toDate", #"toDate",
#"radius", #"radius",
nil];
[criteriaMapping mapKeyPath:#"geometry" toRelationship:#"geometry" withMapping:geoPointMapping];
[objectManager.mappingProvider registerMapping:criteriaMapping withRootKeyPath:#"criteria"];
// Build a serialization mapping by inverting our object mapping. Includes attributes and relationships
RKObjectMapping* criteriaSerializationMapping = [criteriaMapping inverseMapping];
// You can customize the mapping here as necessary -- adding/removing mappings
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:criteriaSerializationMapping forClass:[CRCriteria class]];

Resources