1

Am i doing something weired here ?

my categories get downloaded and mapped,

product also get downloaded and mapped as logging is saying,

but my products are empty under each category, thnx!

{
  "productsCategories" : [
    {
      "product" : [
        {
          "price" : 3.99,
          "title" : "Product A"
        },
        {
          "price" : 3.99,
          "title" : "ProductB "
        }
      ],
      "categoryTitle" : “a category“
    }
  ]
}




RKObjectMapping *productCategoryMapping = [RKObjectMapping mappingForClass:[ProductCategory class]];
[productCategoryMapping addAttributeMappingsFromDictionary:@{
                                                             @"categoryTitle": @"tit"
                                                             }];

RKObjectMapping* productMapping = [RKObjectMapping mappingForClass:[Product class] ];
[productMapping addAttributeMappingsFromDictionary:@{
                                                     @"title": @"tit",
                                                     @"price": @"prc"
                                                       }];
[productCategoryMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"product"
                                                                                       toKeyPath:@"product"
                                                                                     withMapping:productMapping]];


RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:productCategoryMapping method:RKRequestMethodPOST pathPattern:@"productByCategory" keyPath:@"productsCategories" statusCodes:[NSIndexSet indexSetWithIndex:200]];

NSURL *url=[NSURL URLWithString:@"http://www.example.com/REST/v2/"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:url];
[manager addResponseDescriptor:responseDescriptor];

NSMutableDictionary *mutableParameters = [NSMutableDictionary dictionary];
[mutableParameters setValue:@"1" forKey:@"id"];
[manager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:productCategoryMapping method:RKRequestMethodPOST pathPattern:@"productByCategory" keyPath:@"productsCategories" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

[manager postObject:request path:@"productByCategory" parameters:mutableParameters success:^(RKObjectRequestOperation *operation, RKMappingResult *result){

    self.productCategoryArr=result.array;
    [self.tblDetails reloadData];

}failure:^(RKObjectRequestOperation *operation, NSError *error) {

    RKLogError(@"Operation failed with error: %@", error);
    self.productCategoryArr=nil;

}];

the logging says objects are being mapped for each products but I only get

ProductCategory: 0x7bf53510
ProductCategory: 0x7be57f00

arrays and 0 objects in each

1
  • You need to give more information. Add description method implementations to ProductCategory & Product so the log tells you what they contain. Turn on trace logging and provide some details from that. Show how you're testing that the arrays are empty and show the exact JSON which produces your described result of 2 ProductCategory instances. Commented Nov 10, 2014 at 8:36

1 Answer 1

1

Assuming your ProductCategory class has

NSArray *Product
NSString * tit

Create a top level RKObjectMapping like,

RKObjectMapping * ProductCategoryResponseMapping = [RKObjectMapping mappingForClass:  [ProductCategoryResponse class]];

[ProductCategoryResponseMapping addAttributeMappingsFromDictionary:@{
                                                         @"productsCategories": @"productsCategories"
                                                         }];

And create new class ProductCategoryResponse which should have

NSArray * productsCategories

Use ProductCategoryResponseMapping for Response descriptor.

Now your response will have array of productsCategories, each having array of Products and String tit.

Sign up to request clarification or add additional context in comments.

3 Comments

Thnx for ur answer Fahad, I did as u proposed, it works but objects still empty :(
This is not required as the key path of the response descriptor drills past this level in the JSON.
It was a matter of typecasting integers of my json input string, It dit it, thnx anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.