RestKit Object Mapping - Parsing A Nested Array - ios

I'm extremely stuck on mapping a simple JSON object:
{"articles":
[{"id":"354633","lat":"51.501","lng":"-0.125","type":"railwaystation","title":"Westminster tube station","url":"http:\/\/en.wikipedia.org\/w\/index.php?curid=354633","distance":"53m"},
{"id":"92601","lat":"51.5011","lng":"-0.125","type":"railwaystation","title":"17 - Westminster tube station","url":"http:\/\/en.wikipedia.org\/w\/index.php? curid=92601","distance":"62m"},
{"id":"92598","lat":"51.5011","lng":"-0.125","type":"railwaystation","title":"34 -Westminster tube station","url":"http:\/\/en.wikipedia.org\/w\/index.php?curid=92598","distance":"62m"}]
}
I'm really puzzled by the problem I'm having, since this is almost identical to the JSON in the RestKit Mapping Guide.
My code is:
self.articleMapper = [RKObjectMapping mappingForClass:[WikiArticle class]];
[self.articleMapper mapKeyPath:#"title" toAttribute:#"title"];
[[RKObjectManager sharedManager].mappingProvider setMapping:self.articleMapper forKeyPath:#"articles"];
I'm getting the following error in the log:
restkit.object_mapping:RKObjectMappingOperation.m:339 Did not find mappable attribute value keyPath 'title'
restkit.object_mapping:RKObjectMappingOperation.m:557 Mapping operation did not find any mappable content
restkit.object_mapping:RKObjectMapper.m:308 The following operations are in the queue: ()
restkit.object_mapping:RKObjectMapper.m:323 Finished performing object mapping. Results: {}
Thanks in advance for helping me out with something so simple -- I've been banging my head against it for hours!
-Alex

I suppose you call it in a way like this:
[objectManager loadObjectsAtResourcePath:url delegate:self block:^(RKObjectLoader* loader) {
loader.objectMapping = [objectManager.mappingProvider objectMappingForClass:[WikiArticle class]];
}];
Better try:
[objectManager loadObjectsAtResourcePath:url delegate:self];

Related

RestKit PUT request with error mapping

Im making an app that communicates with a rails based backend server.
I already have all the server calls ready and working via RESTKit, but I am having
problems with creating error mappings for my update calls.
My response descriptors for one of my classes
RKResponseDescriptor *descriptor = [RKResponseDescriptor responseDescriptorWithMapping:[Mappings liveViewMapping]
method:RKRequestMethodAny
pathPattern:nil
keyPath:#"event_enriched"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self addResponseDescriptor:descriptor];
RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[Mappings errorMapping]
method:RKRequestMethodAny
pathPattern:nil
keyPath:#"error"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
[self addResponseDescriptor:errorDescriptor];
Edit: I've also tried it with keyPath "errors" and 'nil' .. same results
My error mapping is quite simple:
+ (RKObjectMapping *)errorCollectionMapping {
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[ErrorCollection class]];
NSDictionary *mappingDictionary = #{#"error" : #"message",
#"errors" : #"messages",
};
[errorMapping addAttributeMappingsFromDictionary:mappingDictionary];
return errorMapping;
}
This is how I am trying to update my Book object
[self putObject:book
path:API_BOOK
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
if (success) {
Book *book = [mappingResult.dictionary objectForKey:#"book"];
success(book);
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
if (failure) failure(operation, error);
}
];
But when I get a server error, which should be handled by my response descriptor I get the following error:
Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo=0x7f935d07c7a0 {NSLocalizedDescription=No mappable object representations were found at the key paths searched., NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: book
The representation inputted to the mapper was found to contain nested object representations at the following key paths: error, error_description
This likely indicates that you have misconfigured the key paths for your mappings., keyPath=null, DetailedErrors=(
)}
What am I doing wrong?
I already have some GET requests, that have multiple mappings (non of them is an error mapping yet) which works fine, but can't "replicate" the same behavior with error mapping
oh.. I am using restkit-0.24 :)
edit:
the error responses coming back from my rails server are in this form:
{"errors": ["error1", "error2" ... ] }
or
{"error": "error message" }
i feel so stupid right now ...
The server responses had code 200 and not the 400-499 range that RKStatusCodeClassClientError predicts.

RestKit - Error Domain Code 1001

OK, so I've reviewed almost every single one of the other questions on this site, to no avail.
Here's my JSON that comes back from a REST service:
{
"errors" : {};
"result" : {
"messagebody" : "Hello!";
"timestamp" : "2014-08-21T04:12:28.4689099+00:00";
};
"success" : {};
}
I am trying to pull out the result object via RestKit v0.20.3
Here's a block of my code where it gets configured/executed:
- (void) configureRestKit
{
NSURL *cminstance = [NSURL URLWithString:#"http://<domain>"];
AFHTTPClient *cmclient = [[AFHTTPClient alloc] initWithBaseURL:cminstance];
RKObjectManager *objmgr = [[RKObjectManager alloc] initWithHTTPClient:cmclient];
RKObjectMapping *messageMap = [RKObjectMapping mappingForClass:[Message class]];
[messageMap addAttributeMappingsFromDictionary:#{ #"messagebody" : #"messagebody", #"timestamp": #"timestamp"}];
messageMap.forceCollectionMapping = YES;
RKResponseDescriptor *descriptor =
[RKResponseDescriptor
responseDescriptorWithMapping:messageMap
method:RKRequestMethodGET
pathPattern:#"/v1/app/e51cb2dd24af47a49232b942210e758d/text?f=test"
keyPath:#"result"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objmgr addResponseDescriptor:descriptor];
}
- (IBAction)helloButtonClicked:(id)sender {
[[RKObjectManager sharedManager].HTTPClient setDefaultHeader:#"X-App-ApiKey" value:#"2c130c75dc9f4c2c8ef7c8753e8b7c56"];
NSLog(#"ResponseDescriptors %#", [[RKObjectManager sharedManager] responseDescriptors]);
[[RKObjectManager sharedManager] getObjectsAtPath:#"/v1/app/e51cb2dd24af47a49232b942210e758d/text?f=test" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
_Response.text = [mappingResult.dictionary objectForKey:#"messagebody"];
} failure:^(RKObjectRequestOperation *operation, NSError *error)
{
_Response.text = #"Something went wrong.";
}];
}
Here's the error from the trace:
2014-08-21 00:12:30.653 The-App[13924:3707] E restkit.network:RKObjectRequestOperation.m:208 GET 'http:///v1/app/e51cb2dd24af47a49232b942210e758d/text?f=test' (200 OK / 0 objects) [request=2.7297s mapping=0.0000s total=2.7365s]:
error=Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded." UserInfo=0xb164380 {NSErrorFailingURLStringKey=http:///v1/app/e51cb2dd24af47a49232b942210e758d/text?f=test, NSLocalizedFailureReason=A 200 response was loaded from the URL 'http:///v1/app/e51cb2dd24af47a49232b942210e758d/text?f=test', which failed to match all (0) response descriptors:, NSLocalizedDescription=No response descriptors match the response loaded., keyPath=null, NSErrorFailingURLKey=http:///v1/app/e51cb2dd24af47a49232b942210e758d/text?f=test, NSUnderlyingError=0xb1641c0 "No mappable object representations were found at the key paths searched."}
response.body={"success":{},"errors":{},"result":{"messagebody":"Hello!","timestamp":"2014-08-21T04:12:28.4689099+00:00"}}
Any ideas here on how to troubleshoot? I've been banging my head on this one for a few hours and google has not been my friend.
EDIT: The Response Descriptors
2014-08-22 10:41:21.580 Apprenda-CloudMine-App[10987:60b] ResponseDescriptors (
"<RKResponseDescriptor: 0x17826c180 method=(GET) pathPattern=/v1/app/e51cb2dd24af47a49232b942210e758d/text?f=test keyPath=result statusCodes=200-299 : <RKObjectMapping:0x17826bfc0 objectClass=Message propertyMappings=(\n \"<RKAttributeMapping: 0x1780499f0 timestamp => timestamp>\",\n \"<RKAttributeMapping: 0x17804cf30 messagebody => messagebody>\"\n)>>"
)
Thanks!
-Chris
That error indicates that the path between your request and your response descriptor are not matching. It is subtle, the (0) response descriptors in the error message also indicate to me that there are no response descriptors for that path. For more information on the error messages for RestKit path mismatches see this GitHub issue.
Why then are you getting a path mismatch? After all, the path in your request is exactly the same as your path in your response descriptor. It would seem that your problem is likely the GET parameter specified in the response descriptors path. You need to remove the get parameter from your response descriptors path since it is not technically part of the path.
It should be the following instead.
[RKResponseDescriptor
responseDescriptorWithMapping:messageMap
method:RKRequestMethodGET
pathPattern:#"/v1/app/e51cb2dd24af47a49232b942210e758d/text"
keyPath:#"result"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
Thanks to Flaviu Simihaian for realizing this.

How to add mapping between not embedded JSON objects and relationships using RestKit 0.2 and Core Data?

I'm developing my first iOS application that uses RestKit 0.2 and Core Data. Most of the time I have followed this great tutorial http://www.alexedge.co.uk/blog/2013/03/08/introduction-restkit-0-20/
however it doesn't fit my requirements since it describes embedded JSON objects and I need to create separate objects. Here is a simplified scenario of my app structure:
JSON response:
"application": [
{
"appId": "148",
"appName": …
.
.
.
(more attributes)
}
],
"image": [
{
"imgId": "308",
"appId": "148",
"iType": "screenshot",
"iPath": ..
.
.
.
},
{
"imgId": "307",
"appId": "148",
"iType": "logo",
"iPath": …
.
.
.
}
]
I have created a data model that contains two entities and set the relationship between them in a way that one application may have more than one image and one image may only belong to one application:
I have used mogenerator to create classes represented by entities.
I can successfully map the entity to Application object using the following code:
NSDictionary *parentObjectMapping = #{
#"appId" : #"appId"
};
// Map the Application object
RKEntityMapping *applicationMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([Application class]) inManagedObjectStore:managedObjectStore];
applicationMapping.identificationAttributes = #[ #"appId" ];
[applicationMapping addAttributeMappingsFromDictionary:#{
#"appName" : #"name",
#"appCat" : #"category",
#"appType" : #"type",
#"compName" : #"companyName",
#"compWeb" : #"companyWebsite",
#"dLink" : #"downloadWebLink",
#"packageName" : #"appStoreLink",
#"osMinVer" : #"minRequirements",
#"appCost" : #"cost",
#"appDescription" : #"appDescription"
}];
[applicationMapping addAttributeMappingsFromDictionary:parentObjectMapping];
// Register our mappings with the provider
[manager addResponseDescriptorsFromArray:#[
[RKResponseDescriptor responseDescriptorWithMapping:applicationMapping
pathPattern:nil
keyPath:#"application"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]
]];
Now I have a problem when mapping the JSON to the image object with the appropriate relationship between Image and Application. How can I add the mapping between the two Entities? Is my data model correct or I'm missing something?
You want to use foreign key mapping to allow RestKit to connect the objects.
You need to add an image mapping and it should have an attribute to hold the appId. Then, you need to add a connection relationship:
[imageMapping addConnectionForRelationship:#"application" connectedBy:#{ #"appId": #"appId" }];
Which tells RestKit to use the appId attribute on the image instances to connect to application instances with the corresponding appId and to save the result in the application relationship.
I'm assuming you can't change the data structure of your "image" JSON object, so that will limit RestKit's functionality as far as automatic relational mapping goes. However, you can still set up a relationship yourself.
First, you need to add a field to your data model, appId.
Then, set up your image mapping, like this:
RKEntityMapping* imageMapping = [RKEntityMapping mappingForEntityForName:#"Image" inManagedObjectStore:objectManager.managedObjectStore];
[imageMapping addAttributeMappingsFromDictionary:#{
#"imgId" : #"imageId",
#"iPath" : #"clientPath",
#"iType" : #"type",
#"sPath" : #"serverPath",
#"appId" : #"appId"
}];
imageMapping.identificationAttributes = #[ #"imageId" ];
RKResponseDescriptor *imageResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:imageMapping pathPattern:nil keyPath:#"image" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:imageResponseDescriptor];
After this, your mapping should be done. To fill your local database with your objects, make this call, getObjectsAtPath:
[[RKObjectManager sharedManager] getObjectsAtPath:#"/yourWebServiceURL" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
// success block
NSLog(#"Success!");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
//fail block
}];
This should load all of your objects into CoreData. In another place in your app, maybe even in the success block of the above call, you'll need to connect your Image objects to your Application objects. You'll need fetch requests and a for loop. Just assign every Image's "application" attribute to be the Application object which has the same appId, like this:
myImage.application = fetchedApplicationWithID;
You will then be able to look up the relationship both ways, with [myImage application] and with [myApplication images].

Getting Error While Mapping Objects in RestKit 0.20

I am working on Rest Kit 0.2o, I am using some sample URL for Json format from Web, the response is like Below. I am using Object Mapping. I am following the below link as reference
"https://github.com/RestKit/RestKit/wiki/Object-Mapping" I am using almost same response given in the link.
{
"geonames": [
{
"fcodeName":"capital of a political entity",
"toponymName":"Mexico City",
"countrycode":"MX",
"fcl":"P",
"fclName":"city, village,...",
"name":"Mexiko-Stadt",
"wikipedia":"en.wikipedia.org/wiki/Mexico_City",
"lng":-99.12766456604,
"fcode":"PPLC",
"geonameId":3530597,
"lat":19.428472427036,
"population":12294193
},
{
"fcodeName":"capital of a political entity",
"toponymName":"Manila",
"countrycode":"PH",
"fcl":"P",
"fclName":"city,village,...",
"name":"Manila",
"wikipedia":"en.wikipedia.org/wiki/Manila",
"lng":120.9822,
"fcode":"PPLC",
"geonameId":1701668,
"lat":14.6042,
"population":10444527
}]
}
Below is the code
RKObjectMapping *newsMapping = [RKObjectMapping mappingForClass:[News class]];
[newsMapping addAttributeMappingsFromDictionary:#{
#"fcodeName": #"fcodeName",
#"toponymName": #"toponymName",
#"countrycode": #"countrycode",
#"fcl": #"fcl",
#"fclName": #"fclName",
#"name":#"name",
#"wikipedia":#"wikipedia",
#"lng":#"lng",
#"fcode":#"fcode",
#"geonameId":#"geonameId",
#"lat":#"lat",
#"population":#"population",
}];
RKResponseDescriptor* responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:newsMapping
pathPattern:nil
keyPath:#"newsMapping"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
NSURL* url = [[NSURL alloc]initWithString:#"http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:#[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(#"Load collection of Articles: %#", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
// RKLogError(#"Operation failed with error: %#", error);
NSLog(#"THIS IS TEST %#",error);
}];
[objectRequestOperation start];
"News" is the class i am using and KeyPath i am using is "geonames"
#interface News : NSObject
#property(nonatomic,copy)NSString* fcodeName;
#property(nonatomic,copy)NSString* toponymName;
#property(nonatomic,copy)NSString* countrycode;
#property(nonatomic,copy)NSString* fcl;
#property(nonatomic,copy)NSString* fclName;
#property(nonatomic,copy)NSString* name;
#property(nonatomic,copy)NSString* wikipedia;
#property(nonatomic,copy)NSString* lng;
#property(nonatomic,copy)NSString* fcode;
#property(nonatomic,copy)NSString* geonameId;
#property(nonatomic,copy)NSString* lat;
#property(nonatomic,copy)NSString* population;
After doing all this I am getting below error
2013-05-30 12:06:14.376 TestApp[7140:11603] I restkit.network:RKObjectRequestOperation.m:174 GET 'http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo'
2013-05-30 12:06:15.477 TestApp[7140:12b07] E restkit.network:RKObjectRequestOperation.m:236 Test GET 'http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo' (200 OK / 0 objects) [request=1.0969s mapping=0.0000s total=1.1056s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo=0x7675f20 {DetailedErrors=(
), NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: newsMapping
The representation inputted to the mapper was found to contain nested object representations at the following key paths: status
This likely indicates that you have misconfigured the key paths for your mappings., NSLocalizedDescription=No mappable object representations were found at the key paths searched., keyPath=null}
2013-05-30 12:06:15.477 TestApp[7140:14d03] blockk
2013-05-30 12:06:15.478 TestApp[7140:14d03] erorrrrrr next ifr
2013-05-30 12:06:15.478 TestApp[7140:14d03] erorrrrrr next ifjhkr
2013-05-30 12:06:15.478 TestApp[7140:11603] THIS IS TEST Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo=0x7675f20 {DetailedErrors=(
), NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: newsMapping
The representation inputted to the mapper was found to contain nested object representations at the following key paths: status
This likely indicates that you have misconfigured the key paths for your mappings., NSLocalizedDescription=No mappable object representations were found at the key paths searched., keyPath=null}
Actually in error it is showing keyPath as null in last line But in Browser i am getting the response i have mentioned above.
Please Help
Your response descriptor keypath is wrong. Change to:
RKResponseDescriptor* responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:newsMapping pathPattern:nil keyPath:#"geonames" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
The error you're seeing currently would seem to be because the JSON actually returned is:
{"status":{"message":"the deaily limit of 30000 credits for demo has been exceeded. Please use an application specific account. Do not use the demo account for your application.","value":18}}
Which obviously does not match what you're expecting...

Specify a specific mapping for objects in JSON array in RESTkit?

I'm receiving this JSON from django-rest-framework when I request a list of UserProfile objects:
[
{
"gender": 1,
"show_gender": true,
"show_real_name": true
},
{
"gender": 2,
"show_gender": true,
"show_real_name": true
}
]
But I'm at loss how to configure my mappings with Restkit. Restkit seems to expect a dictionary instead of a list, since it seems to use the dictionary's keys as "KeyPath" to identify the necessary mappings. Is there a way to specify a mapping for the received objects manually?
Got it!
// define a mapping
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[ERUser class]];
[userMapping mapAttributes:
#"gender",
#"show_gender",
#"show_real_name",
nil];
// add the mapping anonymously
[objectManager.mappingProvider addObjectMapping:userMapping];
// tell the mapping provider which mapping to use
RKObjectMapping* articleMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[ERUser class]];
[objectManager loadObjectsAtResourcePath:#"/users" objectMapping:articleMapping delegate:self];

Resources