Rob
Rob

Reputation: 7216

Best practice for lazy loading in RestKit

I have an API that I'm querying where when I query for a list of organizations I get an array of:

{ 
    id: integer,
    name: string
}

But I am also able to get details on each organization which returns a single object like:

{ 
    id: integer,
    name: string,
    description: text,
    visit_count: integer,
    image_url: string
}

How would I set up the object(s) in RestKit for this?

Upvotes: 0

Views: 193

Answers (1)

Mika
Mika

Reputation: 5845

You need to setup the following:

  1. The Object with all the elements you described in your second block
  2. An object manager

    RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:YOUR HOST NAME]];
    objectManager.requestSerializationMIMEType=RKMIMETypeJSON;
    
  3. A mapping object with all the elements in your object

    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[YOUR CLASS class]];
    
  4. The mapping for your object

    [mapping addAttributeMappingsFromArray:@[@"id",
                                             @"name",
                                             @"description",
                                             @"visit_count",
                                             @"image_url"
                                             ]];
    
  5. A response descriptor

    RKResponseDescriptor *responseDesciptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:OBJECT URL keyPath:YOUR KEY PATH statusCodes:nil];
    
  6. Add the response descriptor to the object manager

    [objectManager responseDesciptor];
    
  7. Now you can hit up the server

    [[RKObjectManager sharedManager] postObject:nil path:OBJECT URL parameters:nil
         success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
             self.variable = [NSMutableArray arrayWithArray: mappingResult.array];
         }
     } failure:^(RKObjectRequestOperation *operation, NSError *error) {
    
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An Error Has Occurred" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
    
      }];
    

Upvotes: 1

Related Questions