2

I recently used RestKit to handle my net request affairs. There is a solution for the sort with the sort descriptor. But there is no sort key for the data sent by the server.

How can I keep the data in the same sequence as the server.

There is a solution that I can add a sortID in the object, but this is not very elegant. I want to know if there is any api in RestKit for this problem?

Wain
  • 118,658
  • 15
  • 128
  • 151
Yue Liu
  • 213
  • 2
  • 9

1 Answers1

4

You should add sortID to the object - this is the appropriate solution. To populate it with a value you need to use the @metadata made available to your mappings:

@"@metadata.mapping.collectionIndex" : @"sortID"

This code assumes that you are specifying your mapping with a dictionary (addAttributeMappingsFromDictionary:).

Documented here, the collectionIndex provides you with an NSNumber representing the order of the item in the response data.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • can you share an example for the metadata, or give me a link for this, I have no idea about this. – Yue Liu Nov 27 '13 at 12:05
  • I have been looking into this approach for ourselves. However, the problem arises that when getting the second page of data, this collectionIndex is reset. Any idea for how to get this to work with pagination data too? – DonnaLea Jul 14 '14 at 21:52
  • Previously I have recovered the page id from the request URL and used both the page and the index to perform the sorting @DonnaLea – Wain Jul 15 '14 at 07:46
  • @Wain thanks for that. I have actually been trying to figure out how I can map the page id from the request URL. Currently given up, would love a point in the right direction. I think it's still relevant discussion for this question/answer. – DonnaLea Jul 15 '14 at 22:04
  • How do you handle the page number? Query parameter or path component? Look at RKRoute and @metadata – Wain Jul 15 '14 at 22:36
  • Query paramater: e.g. page=2 I could not see how to map on the fly with that. In the end I used something else which incremented with ongoing requests which works for now, but would love to know nice way. I ended up mapping to the date in the response header @metadata.HTTP.response.headers.date This only works because we are appending results to the one large list. Not the nicest solution, would love to know if I can map to the page number query parameter in the future. – DonnaLea Jul 16 '14 at 16:04
  • 1
    Then you would generally pass the full URL from @metadata to a custom method on your data model class and in that method extract the page number from the query parameters and store it. – Wain Jul 16 '14 at 16:08
  • Wow, you're fast. I had just finished editing my previous comment to include what I ended up doing. Thanks for your insight! – DonnaLea Jul 16 '14 at 16:10