0

I am using RestKIT to make a GET Request for an API that I want to use. Sadly there is no output. This is the output of the GET Request:

{
  "return_code": 0,
  "return_message": "",
  "result": {
    "products_count": 2,
    "product": [
      {
        "id": "2440508995",
        "name": "Long Sweater",
        "description": "This product guarantees warmth in times of need.",
        "price": 8,
        "categories_ids": "73978179"
      },
      {
        "id": "2455113539",
        "name": "Tommy Hilfiger Pants",
        "description": "Exclusive brown pants",
        "price": 13,
        "categories_ids": ""
      }
    ]
  }
}

Obviously I just want an array, where all the products are stored. This is my method to do that.

- (void)loadProducts
{
    RKObjectMapping* resultMapping = [RKObjectMapping mappingForClass:[Result class]];
    [resultMapping addAttributeMappingsFromDictionary:@{
                                                          @"products_count": @"products_count",

                                                          }];

    RKObjectMapping* productMapping = [RKObjectMapping mappingForClass:[Product class]];
    [productMapping addAttributeMappingsFromDictionary:@{
                                                         @"id": @"id",
                                                         @"name": @"name",
                                                         @"description": @"description",
                                                         @"price": @"price",
                                                         @"categories_ids": @"categories_ids"
                                                         }];

    [resultMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"product" toKeyPath:@"product" withMapping:productMapping]];



    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:productMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"result" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    NSURL *URL = [NSURL URLWithString:@"https://api.api2cart.com/v1.0/product.list.json?api_key=//some keyf&store_key=//some key"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
    [objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

        RKLogInfo(@"Load collection of Products: %@", mappingResult.array);
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        RKLogError(@"Operation failed with error: %@", error);
    }];

    [objectRequestOperation start];

}

I made two classes, one for the result that has a NSNumber "products_count" and one Product class with all the product attitudes. The problem is that even though the connection is made and there is no "error", mappingResult.array is apparently empty. What is the mistake in the code?

10
  • You're using the product mapping in the result response descriptor...
    – Wain
    Sep 7, 2015 at 6:50
  • So I have substitute it with resultMapping?
    – Kevin Wu
    Sep 7, 2015 at 7:03
  • That would be the first thing to do. Do you actually need the results container? You could drill down directly to the products with the response descriptor key path...
    – Wain
    Sep 7, 2015 at 8:07
  • OK, I tried both: 1. I changed it to resultMapping and got the error that the Product class is not key-value compliant. 2. I reverse the change (so now its product mapping again )and put as key path response.product; however it tells me that only available key paths are result, return_code and return_message
    – Kevin Wu
    Sep 7, 2015 at 9:05
  • response.product ? did you mean result.product ?
    – Wain
    Sep 7, 2015 at 10:07

1 Answer 1

1

You don't actually need your Result class as the array will already give you the account and you can go direct to the array.

So, your initial problem is that you're using the wrong mapping in the response descriptor. You just need to switch to the result mapping.

You can use the product mapping in the response descriptor, but you should change the key path to result.products.

Your product mapping also has an issue because description is an existing method on all NSObject subclasses so you need to change your usage of it by adding a property called something like overview or title.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.