Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would suggest the following structure (using Instagram as an example):</p> <p>1) first of all split all your requests based on the "Resource" you use e.g. Users/Comments/Likes, etc</p> <p>2) For each "Resource" create a separate class, subclass of RKObjectManager, for example UsersManager, CommentsManager, LikesManager (all inherited from <strong>RKObjectManager</strong>)</p> <p>3) Define extra methods on every manager, that you will use from View Controller. For example, for loading "likes" resource for user, you'd define this method in LikesManager (or UserManager) -- this is very opinionated decision, and it's really up to you.</p> <pre><code>- (void)loadLikesForUser:(User *)user success:(void (^)(NSArray *likes))successCallback failure:(void (^)(NSError *error))failureCallback; </code></pre> <p>4) Implement this method and call appropriate methods using <strong><em>self</em></strong>, because you've created a subclass of <strong>RKObjectManager</strong> class and have access to all basic methods.</p> <blockquote> <pre><code> [self getObjectsAtPath:@"/resources/" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { // pass mappingResult.array to the successCallback } failure:^(RKObjectRequestOperation *operation, NSError *error) { // pass error object to the failureCallback }]; </code></pre> </blockquote> <p>5) In your view controller, you'd call this like</p> <pre><code> [[LikesManager sharedManager] loadLikesForUser:user success:^(NSArray *likes) { self.likes = likes; // work with likes } failure:^(NSError *error) { // handle error }]; </code></pre> <p>Notice that you don't do any stuff with NSNotification, as it'll be a bad design decision in this kind of situations.</p> <p>Generally speaking you can put all of your requests in a subclass of <strong>RKObjectManager</strong>, if you have very few of them, but if you have more than 5-6 requests, you'll find it tedious and hard to manage to keep all of them in one file. So that's why I suggest to split them based on a Resource.</p> <p><strong>UPDATE</strong> Based on questions asked in comments, providing answers here</p> <p>a) Where to set Base URL?</p> <p>Base URL is a property on instance of RKObjectManager, so you definitely want to set it before making any requests to API. To me ideal place would be at the initialization of RKObjectManager instance.</p> <p>b) Where to define Object Mapping?</p> <p>Again it's up to you. Answer to this question is very opinionated. I'd consider 2 options:</p> <ol> <li>create a separate class to hold all your objects related to mappings, like MappingProvider. Then whenever you create RKResponseDescriptor or RKRequestDescriptor, you'd just access properties of MappingProvider to get your mappings.</li> <li>Define mappings in manager's class, because you'll assign them to the RKResponseDescriptor instances that will be used in this manager.</li> </ol> <p><strong>UPDATE</strong>: Check out this blog post on RestKit setup: <a href="http://restkit-tutorials.com/code-organization-in-restkit-based-app/" rel="noreferrer">http://restkit-tutorials.com/code-organization-in-restkit-based-app/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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