1

我使用了 RestKit 并与托管对象进行了映射。然后我使用该postObject方法,但在检索主体时遇到问题,因为它映射为空。

 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];
        }];

编辑

      //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"
                    }
                }
            ]
        }
    ]
}
4

1 回答 1

1

您需要提供Search对象,而不是nil在调用时提供postObject(当前您有postObject:nil)。这告诉 RestKit 使用哪个请求映射来发送请求。由于未设置它,服务器可能不会拥有所有必需的信息(无法通过提供的信息来判断)。

于 2013-12-16T16:12:29.553 回答