Jed Lau
Jed Lau

Reputation: 326

How do I prevent multiple RKResponseDescriptors from matching?

I am using RestKit 0.20.0rc1 in an app that uses 2 entities:

  1. A "note" (NoteClass).
  2. A "set" (SetClass), which contains a collection of notes.

I have the following 2 response descriptors (among others):

// GET /sets/:setID/notes
// Get a set's notes. Response looks like this:
//   {"notes": [ (array of NoteClass dictionaries) ],
//               ...more stuff...
//   }
RKResponseDescriptor *noteResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[NoteClass rkEntityMapping]
                                                             pathPattern:@"/sets/:setID/notes"
                                                                 keyPath:@"notes"
                                                             statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:noteResponseDescriptor];

// GET /sets/:setID
// Get information about a set. Response looks like this:
//   {"name": "My Set",
//    "numNotes": 3,
//    ...more stuff...
//   }
RKResponseDescriptor *setResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[SetClass rkEntityMapping]
                                                             pathPattern:@"/sets/:setID"
                                                                 keyPath:nil
                                                             statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:setResponseDescriptor];

When I request "/sets/:setID/notes", the noteResponseDescriptor matches (expected). However, the setResponseDescriptor also matches (unexpected). I believe this is because the response descriptor's path pattern matches the substring "/sets/:setID", and because key path is nil. As a result, when I make the request, the RKMappingResult I get back contains an array of NoteClass objects (expected) and a single empty SetClass object (unexpected).

How do I prevent the setResponseDescriptor from matching this notes endpoint? I can't add a key path to the setResponseDescriptor (yet), so my preference is for a solution that allows me to say something like "match /sets/:setID$", where "$" designates the end of the URL.

Upvotes: 4

Views: 1359

Answers (1)

Mihai Damian
Mihai Damian

Reputation: 11432

Turns out that there is now way to prevent multiple matches for your example (see the discussion here).

While a solution is on the way you have a few options for fixing this:

  1. Change your API urls so it doesn't lead to ambiguities
  2. Check the type of the mapped result objects and discard any unexpected objects
  3. Modify [RKResponseDescriptor matchesPath:] as suggested in the discussion thread.

Each solution is flawed in it's own way though.

Upvotes: 1

Related Questions