RestKit 0.20.2生成错误的JSON输出,用于在post / put操作中发送子对象集合

时间:2013-06-24 05:27:56

标签: restkit-0.20

我有4个NSManagedObjects,即Item,Listing,PaymentMethod和HandlingTime,这样Item与1-1具有1-1关系,它与PaymentMethod有1-n关系,而Listing与HandlingTIme有1-1关系。当我尝试发布/放置项目时,以下映射适用于Item-Listing和Listing-HandlingTime关系,用于进行项目post / put服务器操作,但不会为Listing-PaymentMethods生成正确的JSON。

NSDictionary *itemRKRequestMapping = @{
        @"id" : @"eid",
        @"title" : @"title",
        @"listing.id" : @"listing.eid",
        @"listing.title" : @"listing.title",
        @"listing.item.id" : @"listing.item.eid",
        @"listing.handlingTime.id" : @"listing.handlingTime.eid",
        @"listing.handlingTime.title" : @"listing.handlingTime.title",
        @"listing.paymentMethods.id" : @"listing.paymentMethods.eid",
        @"listing.paymentMethods.title" : @"listing.paymentMethods.title"
};

在调用

时,它为RKRequest操作生成以下JSON
[[RKObjectManager sharedManager] putObject:self.item path:@"items/1" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
            NSLog(@"Success"); }              failure:^(RKObjectRequestOperation *operation, NSError *error) {
            NSLog(@"Failure saving item: %@", error.localizedDescription);
        }];

在paymentMethods的正确JSON中。

{"id":1, "title":"item1", "listing":{"id":1,"item":{"id":1},"handlingTime":{"id":1, "title":"20 days"},"paymentMethods":{"id" : [1,2],"title":["VISA","MASTER"]} } }

以下是paymentMethods所需的正确JSON输出。

{"id":1, "title":"item1", "listing":{"id":1,"item":{"id":1},"handlingTime":{"id":1, "title":"20 days"},"paymentMethods": [ { "id":1, "title": "VISA"},  { "id":2, "title": "MASTER"}  ]   } }

GET操作适用于此对象模型,因为它使用relationshipMappingFromKeyPath设置项目列表关系,如下所示。

[itemMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"listing" toKeyPath:@"listing" withMapping:listingMapping]];
[listingMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"handlingTime" toKeyPath:@"handlingTime" withMapping:handlingTimeMapping]];
[listingMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"paymentMethods" toKeyPath:@"paymentMethods" withMapping:paymentMethodMapping]];

任何人都可以指出上面RKRequest操作的映射错误吗?

1 个答案:

答案 0 :(得分:0)

您的问题是该关系只包含一个项目,因此RestKit不知道它应该映射到一个数组。要教授RestKit,您需要分离相应键的映射,为其显式创建属性映射,将映射设置为forceCollectionMapping,然后将其添加到itemRKRequestMapping

forceCollectionMapping docs are here