1

I have 4 NSManagedObjects i.e. Item, Listing, PaymentMethod and HandlingTime such that Item has 1-1 relation with Listing which has 1-n relation with PaymentMethod and Listing has 1-1 relation with HandlingTIme. When I try to post/put an item, following mapping works for Item-Listing and Listing-HandlingTime relationship for making item post/put server operations but does not generate correct JSON for Listing-PaymentMethods.

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

It produces the following JSON for RKRequest operation when calling

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

In correct JSON for paymentMethods.

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

Following is the desired correct JSON output for paymentMethods.

{"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 operation works fine for this object model as thats setup using the relationshipMappingFromKeyPath for item- listing relation as shown below.

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

Can any one please point out the mistake in the mappings for RKRequest operations above?

4

1 回答 1

0

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

forceCollectionMapping文档在这里

于 2013-06-24T07:34:36.780 回答