0

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.

4

1 回答 1

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

简单地说:

  • request: 我去哪里获取数据
  • paginationMapping:在响应中,我如何知道我们在哪个页面
  • responseDescriptors:在回复中,我如何获取您感兴趣的数据

使用objectManager将使您的代码更简单,但如果您完成响应描述符,您当前所拥有的应该可以工作。目前,如果您要运行,您可以向 RestKit 提供足够的信息来发出请求,[self.paginator loadPage:1];但您没有提供有关如何从响应中映射对象以返回到完成块NSArray *objects参数的信息。

于 2013-05-04T07:04:49.860 回答