Note that there are some explanatory texts on larger screens.

plurals
  1. PORestKit Mapping objects within a default relationship using a specified primary key reference
    text
    copied!<p>I am currently developing an iOS Messenger application and I would like to talk about these two classes relationship: <code>MessengerConversation</code> and <code>MessengerMessage</code>.</p> <p>Supposing that I already have a local MessengerConversation instance which can have many MessengerMessage instances related into its <code>messages</code> relationship property, I would like to request and mapping the following JSON payload:</p> <p>Request: GET /conversations/:conversationID/msgs <br> Response:</p> <pre><code>{ "messages": [ { ... "messageid": n, "content": "..." ... }, ... ] } </code></pre> <p>As the response JSON payload didn't indicate which conversation the delivered messages are from. I used the following approach to fix this issue into my <code>MessengerManager</code> class (Responsible for interacting with the shared RKObjectManager instance):</p> <pre><code>- (void)objectLoader:(RKObjectLoader *)objectLoader willMapData:(inout id *)mappableData { // // GETs Resource Paths. if ([objectLoader method] == RKRequestMethodGET) { if ([pathMatcherConversationsMessagesGET matchesPath:objectLoader.resourcePath tokenizeQueryStrings:NO parsedArguments:Nil]) { // // Get requested conversationID from the following resource path Pattern: // kMessengerManagerResourcePathMessages: /conversations/:conversationID/msgs NSNumber *currentConversationID = Nil; NSDictionary *arguments = Nil; BOOL isMatchingPattern = [pathMatcherConversationsMessagesGET matchesPattern:kMessengerManagerResourcePathConversationsMessagesGET tokenizeQueryStrings:YES parsedArguments:&amp;arguments]; if (isMatchingPattern) { currentConversationID = [arguments objectForKey:@"conversationID"]; // // Get the original returned array of messages: NSArray *origMessagesArray = [*mappableData valueForKeyPath:@"messages"]; NSMutableArray *reformattedData = [NSMutableArray arrayWithCapacity:[origMessagesArray count]]; // // Create copies of objects adding the already knew reference. for (NSDictionary *origMessagesArrayObject in origMessagesArray) { NSMutableDictionary *newMessagesArrayObject = [origMessagesArrayObject mutableCopy]; [newMessagesArrayObject setObject:currentConversationID forKey:@"conversationid"]; [reformattedData addObject:newMessagesArrayObject]; } // // Replace the new objects instead of the response objects [*mappableData setObject:reformattedData forKey:@"messages"]; } } } } </code></pre> <p>And so everything worked properly. That is, all loaded MessengerMessages from the specified MessengerConversation (into the RKObjectLoader resource path) are being inserted into the wanted relationship. </p> <p>Now comes the real problem, as I am working with my MessengerManager class which adopts the RKObjectLoaderProtocol, I could implement the <code>objectLoader:willMapData:</code> method. But as my View classes are using the <code>RKFetchedResultsTableController</code> and every table controller instance is also its RKObjectLoader delegate, I don't know which would be the best practice to enable a RKFetchedResultsTableController instance to update a received JSON payload before the mapping operation.</p> <p>Shall I subclass it?</p> <p>And are there better ways to map received objects into a pre-defined object specified by a RKObjectLoader resource path (e.g: GET /conversations/2/msg where all resulted messages should be mapped inside the defined MessengerConversation object with its primary key value equal to 2)?</p> <p>Best regards,<br> Piva</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload