Will RestKit's dynamic mapping solve this comp

2019-03-06 03:11发布

问题:

I am using RestKit in my app, which needs to use an existing synchronization service that structures the incoming data this way:

{
  "timestamp": 000000000001,
  "status" : 0,
  "syncData":[
    {
      "errors":[],
      "rows":[ {"name":"AAA", ...},
               {"name":"BBB", ...}, 
               ...],
      "rtype":"FOO" },
    {
      "errors":[],
      "rows":[ {"id":1, "description":"ZZZ", ....},
               {"id":2, "description":"YYY", ....}, 
               ...],
      "rtype":"BAR"
    }, ...

I'm new to RestKit and trying to figure out the best way to solve this problem, and the complementary problem of sending this same structure of data back to the server. I'm using Core Data with RestKit.

I've mapped a SyncResponse entity to hold the top level data, and what I want to get out of this is a collection of FOO objects, "AAA", "BBB", etc., and a collection of BAR objects, "ZZZ", "YYY", etc., and a few dozen other collections of objects whose Class is indicated by the "rtype" field.

I've read the doc section on dynamic mapping and some example code and postings here, but I don't see how dynamic mapping works in this case as it is not of the {"a":{is A}, "b":{is B}} format. Is this possible using dynamic mapping, and if so, what concepts am I missing here?

Assuming it is possible, how do I, starting with collections of FOOs and BARs send data back, of course replacing the SyncResponse with something like a SyncUpdateRequest wrapper?

回答1:

I don't think you'll be able to do this using a set of mappings alone.

Your best option may be to create your mappings for each item and one for the overall structure. The overall mapping just extracts the array as an NSArray of dictionaries. Once you have the array you can iterate over it yourself, check the type and then apply an RKMapperOperation to perform the mappings.


For sending your update request, I'd look at it as a quite separate thing. I'd build an array of dictionaries where the dictionaries have 'plain' key / value pairs for some information and 'complex' key / value pairs for the rows. Your request mapping is then in terms of this array of dictionaries (which cover the custom parts) and the rows (which should be the inverse of your response mapping for the class). Then RestKit should be able to handle it in the standard way (compared to the complexity of your response mapping above).