1

I have used RestKit and made a mapping with a managed object. I then use the postObject method but I have a problem when retrieved the body as it maps to null.

 RKObjectManager  *objectManager = [RKObjectManager managerWithBaseURL:baseURL];

// Serialize to JSON
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;

[RKObjectManager setSharedManager:objectManager];

RKEntityMapping *searchInfoMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([Search class]) inManagedObjectStore: objectManager.managedObjectStore];
searchInfoMapping.identificationAttributes = @[ @"faresQueryType" ];

[searchInfoMapping addAttributeMappingsFromDictionary:@{
    @"id" : @"ID",
    @"type" : @"Type",
    @"count" : @"Count",
    @"route” : @"route”,
}];

RKObjectMapping     *searchInfoRequestMapping =[RKObjectMapping requestMapping];
[searchInfoRequestMapping addAttributeMappingsFromDictionary:@{
    @"id" : @"ID",
    @"type" : @"Type",
    @"count" : @"Count",
    @"route” : @"route”,
}];

//Data mapping is a method that returns an RKObjectMapping for my model
RKResponseDescriptor * searchDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:searchInfoMapping pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptorsFromArray:@[searchDescriptor,]];

//Inverse mapping, to perform a POST

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:searchInfoRequestMapping  objectClass:[Search class] rootKeyPath:nil];
[objectManager addRequestDescriptor:requestDescriptor];


[appDelegate.objectManager postObject:nil path:@"routes" parameters:postParameters success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
            NSLog(@"Success case %@",mappingResult);
        } failure:^(RKObjectRequestOperation *operation, NSError *error) {
            NSLog(@"Failure in Flight Mapping %@",error);
            [alert show];
        }];

Edit

      //JSON Result 

    {
    "currency": {
        "code": "USD",
        "name": "U.S. Dollar",
        "symbol": "US$",
        "exchange_rate": 1
    },
    "routes": [
        {
            "id": "MS648-2022:MS985-2110",
            "fares": [
                {
                    "price": 745.32,
                    "description": "Economy",
                    "deeplink": "http://www.wego.com/flights/providers/2/deeplinks?search_id=CQtxCXQCRfmwhp72PAjopQ&trip_id=RUH:NYC:2013-12-20&fare_id=sky-tours.com:0&route=RUH-JFK",
                    "provider_code": "sky-tours.com",
                    "deeplink_params": {
                        "trip_id": "RUH:NYC:2013-12-20",
                        "route": "RUH-JFK",
                        "search_id": "CQtxCXQCRfmwhp72PAjopQ",
                        "fare_id": "sky-tours.com:0"
                    }
                }
            ]
        }
    ]
}
Wain
  • 118,658
  • 15
  • 128
  • 151
wod
  • 812
  • 1
  • 10
  • 23
  • Give more details of the JSON sent and received. – Wain Dec 16 '13 at 16:12
  • The `searchInfoMapping` seems to have no comparison to the result JSON. How is the `Search` object supposed to relate to the returned JSON? I would expect the search object to have a relationship to the `routes` info in the JSON... – Wain Dec 17 '13 at 10:38
  • I got you before the root value but now i need to retrieved the "routes" , simply consider "Search" is "routes" . – wod Dec 17 '13 at 10:59
  • `Search` is a single object being posted, so RestKit wants to map the POST back into that single object. Something like overall detail into that object and then the `routes` into a relationship. Can you work like that? – Wain Dec 17 '13 at 11:02
  • Do you have a good example in RestKit JSON using POST with CoreData? – wod Dec 17 '13 at 11:07
  • RestKit github page has example code. I'm more interested in modifying your data model and then mapping the data you have into it. To me, and RestKit, it makes sense to have an object recording the search details and linking to the obtained results. – Wain Dec 17 '13 at 11:12
  • What is the object attribute should be send to postObject method – wod Dec 17 '13 at 11:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43325/discussion-between-wain-and-wod) – Wain Dec 17 '13 at 12:00

1 Answers1

1

You need to supply the Search object instead of nil when you call postObject (currently you have postObject:nil). This tells RestKit which request mapping to use to send the request. As a result of not setting it the server will likely not have all of the required information (it's impossible to tell with the information provided).

Wain
  • 118,658
  • 15
  • 128
  • 151
  • But i call the postObject method in viewcontroller and mapping in appDelegation – wod Dec 16 '13 at 16:20
  • 1
    It doesn't matter where you call it from. Do you have a `Search` instance that you should be passing? What are you expecting to be sent and received? What is `postParameters`? – Wain Dec 16 '13 at 16:22
  • Yes i have Search instance in CoreData managed object , and postParameters is NsMutableDictionary.I have tested now without using coreData and mapping it is work fine.But when i use mapping with above code sometimes retrieved data and sometimes the relationship data isn't retrieved – wod Dec 16 '13 at 16:27
  • I think you should add a mapping for `faresQueryType` - and show details of what you send and receive... – Wain Dec 16 '13 at 16:30
  • Could you help me by provide me full example RestKit JSON POST data with mapping and response & request descriptor? – wod Dec 16 '13 at 16:41
  • Now i got this error after update may code with Search class for '__NSDictionaryM' object. Mapping values from object Search ((null)) to object { } with object mapping (null) – wod Dec 16 '13 at 16:58
  • @Wain would you chat now about this issue ? – JAHelia Dec 17 '13 at 21:41
  • @JAHelia, what do you need to know (and how related to this question)? – Wain Dec 17 '13 at 21:44
  • @Wain I'm involved in the same issue with my friend wod, is it ok to chat for couple minutes ? – JAHelia Dec 17 '13 at 21:44
  • Haven't created a room before, this might work: http://chat.stackoverflow.com/rooms/43373/chitchat – Wain Dec 17 '13 at 21:47
  • @Wain are you available for chitchat for couple of minutes ? – JAHelia Dec 18 '13 at 11:29
  • Not at the time of day, sorry. If you have one quick question (though I might not reply for an hour or two...) – Wain Dec 18 '13 at 11:47
  • @Wain the problem looks like this: http://stackoverflow.com/questions/16632743/restkit-fetching-empty-results-on-subsequent-fetches the response body contains arrays along with some other stuff (this is clearly depicted in this screenshot: https://dl.dropboxusercontent.com/u/57066689/jsonArc.png ), the problem lies within all arrays in the JSON response, sometimes these arrays return empty and sometimes return with data, I've subclassed RKObjectManager and set cache policy to NSURLRequestReloadIgnoringLocalAndRemoteCacheData in requestWithObject method but to no avail. – JAHelia Dec 18 '13 at 13:58
  • @Wain there is no problem with mappings since data when returned, the response is mapped and persisted to core data entities as expected. why do there arrays return as empty in some subsequent calls. p.s. I've updated RestKit to the latest version 0.20.3 and we have a doubt that it is an NSURLConnection caching issue as commented in the question http://stackoverflow.com/questions/16632743/restkit-fetching-empty-results-on-subsequent-fetches – JAHelia Dec 18 '13 at 13:59
  • Do you mean that the response from the server is empty sometimes? Or the response is the same as the previous response? This is from the server, before RestKit starts processing the data... – Wain Dec 18 '13 at 14:24
  • the response contains many arrays and dictionaries (as in the posted screenshot above) all arrays return empty (zero objects in them) sometimes. by the way, this isn't a server issue because my friend connects to this web service on Android and he didn't face such issue. – JAHelia Dec 18 '13 at 17:43
  • It shouldn't be a cache issue, check the values that are being sent to the server. A cache problem would mean the server wasn't called at all. – Wain Dec 18 '13 at 18:18
  • the values are OK ( i enabled rest kit logging - trace level) all values in the request header and body are correct. – JAHelia Dec 18 '13 at 18:42
  • here is a WiredShark screenshot for the empty returned array: https://dl.dropboxusercontent.com/u/57066689/noRoutes.png and here is a screenshot for the array when it got returned with objects: https://dl.dropboxusercontent.com/u/57066689/withRoutes.png – JAHelia Dec 19 '13 at 10:32
  • So that would seem to prove is has nothing to do with RestKit processing the response (because the response is different and empty). Which suggests it is something to do with the request or the server. – Wain Dec 19 '13 at 10:50
  • @Wain yes exactly, it is something to do with the request, the web service has no problems when invoked by PostMan plug-in for Chrome, so the request is the suspect. are you available for chat for 5 minutes ? – JAHelia Dec 19 '13 at 11:36