HCdev
HCdev

Reputation: 550

RestKit ResponseDescriptors for Paging

I've recently upgraded a project from using RestKit 0.1x to 0.2x and am trying to get my head around the changes particularly in paging.

Is someone able to explain to me the following commented code? It's from RKPaginator.h:

/**
 Initializes a RKPaginator object with the a provided patternURL and mappingProvider.

 @param request A request with a URL containing a dynamic pattern specifying how paginated resources are to be acessed.
 @param paginationMapping The pagination mapping specifying how pagination metadata is to be mapped from responses.
 @param responseDescriptors An array of response descriptors describing how to map object representations loaded by object request operations dispatched by the paginator.
 @return The receiver, initialized with the request, pagination mapping, and response descriptors.
 */
- (id)initWithRequest:(NSURLRequest *)request
    paginationMapping:(RKObjectMapping *)paginationMapping
  responseDescriptors:(NSArray *)responseDescriptors;

In particular, what I'm trying to grasp is the role of responseDescriptors.

What is a Response Descriptor? Is it the mapping of the actual objects inside the paginators object collection returned from the query? If so - I've got this mapped in the paginationMapping. Is it then the status codes? I've been through the documentation and just don't get it.

I'd really appreciate a pointer or link to an example.. I can clarify with my existing code - this is the code to load an activity stream depending on what type of stream the user has chosen... activity, project list or favourites (list of sightings)..

-(void)setPaginatorForStream:(NSString*)streamName {

    [BBLog Log:@"BBStreamController.setPaginatorForStream:"];
    [BBLog Debug:@"streamName:" withMessage:streamName];

    __weak typeof(self) weakSelf = self;

    NSString *streamUrl = [NSString stringWithFormat:@"/%@?PageSize=:perPage&Page=:currentPage", streamName];

    if (!self.paginator) {

        RKObjectManager *objectManager = [RKObjectManager sharedManager];
        RKObjectMapping *paginationMapping = nil;

        // TODO: Create response descriptors for these puppies:

        if([streamName isEqualToString:@"favourites"]){ // sightings
            paginationMapping = [RKObjectMapping mappingForClass:[BBSightingPaginator class]];

            self.paginator = [[BBSightingPaginator alloc]initWithRequest:[NSURLRequest requestWithURL:[[NSURL alloc]initWithString:streamUrl]]
                                                       paginationMapping:paginationMapping
                                                     responseDescriptors:nil
                                                             andDelegate:weakSelf];
        }
        else if([streamName isEqualToString:@"projects"]){
            paginationMapping = [RKObjectMapping mappingForClass:[BBProjectPaginator class]];

            self.paginator = [[BBProjectPaginator alloc]initWithRequest:[NSURLRequest requestWithURL:[[NSURL alloc]initWithString:streamUrl]]
                                                       paginationMapping:paginationMapping
                                                     responseDescriptors:nil
                                                             andDelegate:weakSelf];
        }
        else {
            paginationMapping = [RKObjectMapping mappingForClass:[BBActivityPaginator class]];

            self.paginator = [[BBActivityPaginator alloc]initWithRequest:[NSURLRequest requestWithURL:[[NSURL alloc]initWithString:streamUrl]]
                                                       paginationMapping:paginationMapping
                                                     responseDescriptors:nil
                                                             andDelegate:weakSelf];
        }

        self.paginator.perPage = 20;

        [self.paginator setCompletionBlockWithSuccess:^(RKPaginator *paginator, NSArray *objects, NSUInteger page) {
            [weakSelf.tableItems addObjectsFromArray:objects];
            [weakSelf.tableView reloadData];

        } failure:^(RKPaginator *paginator, NSError *error) {
            NSLog(@"Failure: %@", error);
        }];
    }
}

I've followed smakman's example on this post (https://github.com/RestKit/RestKit/issues/1035) to get this far but I am instantiating my paginators from existing mapped objects.

As you can see, I've nil'd out the responseDescriptors... what would I put there?

Many thanks, Hamish.

Upvotes: 0

Views: 973

Answers (1)

Wain
Wain

Reputation: 119031

- (id)initWithRequest:(NSURLRequest *)request
    paginationMapping:(RKObjectMapping *)paginationMapping
  responseDescriptors:(NSArray *)responseDescriptors;

Put simply:

  • request : where do I go to get the data
  • paginationMapping : in the response, how do I tell what page we're at
  • responseDescriptors : in the response, how do I get the data you're interested in

Working with the objectManager would make your code simpler, but what you have currently should work if you complete the response descriptor. Currently, you give RestKit enough information to make the request if you were to run [self.paginator loadPage:1]; but you don't give it information on how to map objects from the response to return to the completion block NSArray *objects parameter.

Upvotes: 1

Related Questions