4

Let's say for example that we have a response like this:

{
   "authors":[
      {
         "id":"9",
         "name":"Some User",
         "email":"some@email.com"
      }
   ],
   "comments":[
      {
         "id":"5",
         "from":"Some Other User #1",
         "content":"Some comment"
      },
      {
         "id":"12",
         "from":"Some Other User #2",
         "content":"Some other comment"
      }
   ],
   "posts":[
      {
         "id":"1",
         "title":"My new post",
         "links":{
            "author":"9",
            "comments":[
               "5",
               "12"
            ]
         }
      }
   ]
}

In an ordinary response, the author and comments might be nested in the posts key and RestKit can easily connect a relationship from the Post class to an associated Author and/or Comment class so you can do post.author and easily get the data you need. However, if you're not using Core Data and therefore cannot use RKConnectionDescription, the response shown above does not allow easy mapping this way.

My question is: If you're using RKObjectMapping w/ in-memory objects, is there a way in RestKit to connect a relationship between the Post model and the associated Author and multiple Comment objects? Ideally, I'd like to have post.author that returns an Author instance and post.comments returns an NSSet or NSArray of Comment instances.

I've found a similar issue on the RestKit repo, but no information to work off of there. I've also researched quite a bit, but I can't come up with a built-in way of doing this using RKObjectMapping in RestKit. Any guidance you can provide is greatly appreciated.

ZeNewb
  • 435
  • 5
  • 17
  • 1
    "Is there a way to do..." what? Can you describe your goal in a bit more detail? – jscs Jan 25 '14 at 21:39
  • 1
    You've said nothing about what the mapped result should be or what mappings you currently have... – Wain Jan 25 '14 at 22:47
  • @Wain Sorry about that! I just want to be able to have the `posts` have nested `author` and `comments` objects as if it were a normal nested dictionary – ZeNewb Jan 26 '14 at 01:26
  • @JoshCaswell Sorry about that, edited the post to clarify – ZeNewb Jan 26 '14 at 01:30
  • I've clarified the question a bit, please consider re-opening and any help is greatly appreciated! – ZeNewb Jan 27 '14 at 15:17
  • RestKit supports dynamic mapping as described here https://github.com/RestKit/RestKit/wiki/Object-mapping#dynamic-object-mapping. I'm not sure it fits your problem. Just as possible variant, if you haven't explored it yet. – vokilam Feb 07 '14 at 07:18

1 Answers1

1

Have you come across Metadata Mapping as of yet? I believe you can take advantage of the @root key in order to create the connection you're going for (i.e. @"@root.authors.id" ).

I can't seem to find direct documentation on this, but you can see the pull here and there is table in RestKit For iOS if you have / get access.

nomad00
  • 401
  • 3
  • 10