我最近将一个项目从使用RestKit 0.1x升级到了0.2x,并且正在设法使我特别关注分页中的更改。
有人可以向我解释以下注释的代码吗?来自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;
特别是,我要掌握的是responseDescriptors的角色。
什么是响应描述符?它是查询返回的分页器对象集中的实际对象的映射吗?如果是这样-我已经在paginationMapping中映射了它。然后是状态码吗?我已经看过文档,只是不了解。
我非常感谢指向示例的指针或链接。.我可以用我现有的代码进行澄清-这是根据用户选择的流类型加载活动流的代码...活动,项目列表或收藏夹(目击清单)
-(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);
}];
}
}
我已经按照这篇文章(https://github.com/RestKit/RestKit/issues/1035)上smakman的示例进行了深入了解,但是我正在从现有映射对象中实例化分页器。
如您所见,我没有列出responseDescriptors ...我该怎么办?
非常感谢,
哈米什
最佳答案
- (id)initWithRequest:(NSURLRequest *)request
paginationMapping:(RKObjectMapping *)paginationMapping
responseDescriptors:(NSArray *)responseDescriptors;
简单地说:
request
:我要去哪里获取数据paginationMapping
:在响应中,我如何知道我们在哪一页responseDescriptors
:在响应中,如何获取您感兴趣的数据使用
objectManager
将使您的代码更简单,但是如果您完成响应描述符,则当前使用的代码应该可以工作。当前,如果要运行[self.paginator loadPage:1];
,您将为RestKit提供足够的信息来发出请求,但是您没有为它提供有关如何从响应中映射对象以返回到完成块NSArray *objects
参数的信息。关于iphone - 用于分页的RestKit ResponseDescriptor,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16369860/