1

我遇到了分页问题。

当我去 /?PageSize=:perPage&Page=:page" 我会得到一个像这样的 json 响应:

API Json 响应

我想使用以下分页器进行映射:

/* BBActivityPaginator */

RKObjectMapping *activityPaginationMapping = [RKObjectMapping mappingForClass:[BBActivityPaginator class]];
[activityPaginationMapping addAttributeMappingsFromDictionary:@{
 @"Page" :@"currentPage",
 @"PageSize" :@"perPage",
 @"TotalResultCount" :@"objectCount"
 }];
[activityPaginationMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"PagedListItems" toKeyPath:@"activities" withMapping:activityMapping]];
[manager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:activityPaginationMapping
                                                                       pathPattern:@"/?PageSize=:perPage&Page=:page"
                                                                           keyPath:@"Model.Activities"
                                                                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

与此映射有关:

/* BB活动 */

RKObjectMapping *activityMapping = [RKObjectMapping mappingForClass:[BBActivity class]];
[activityMapping addAttributeMappingsFromDictionary:@{
    @"Id" :@"identifier",
    @"CreatedDateTime" :@"createdOn",
    @"Description" :@"description",
    @"CreatedDateTimeOrder" :@"order",
    @"Type" :@"type",
    @"DeletedActivityItem.Message" :@"deleted"
 }];
[activityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"User" toKeyPath:@"user" withMapping:userMapping]];
[activityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"ObservationAdded.Observation" toKeyPath:@"observation" withMapping:observationMapping]];
[activityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"SightingNoteAdded.SightingNote" toKeyPath:@"observationNote" withMapping:observationNoteMapping]];
[activityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"SightingNoteAdded.Sighting" toKeyPath:@"observationNoteObservation" withMapping:observationMapping]];
[activityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"IdentificationAdded.Sighting" toKeyPath:@"identificationObservation" withMapping:observationMapping]];
[activityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"IdentificationAdded.Identification" toKeyPath:@"identification" withMapping:identificationMapping]];
[activityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"PostAdded.Post" toKeyPath:@"post" withMapping:postMapping]];
[manager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:activityMapping
                                                                       pathPattern:nil
                                                                           keyPath:nil
                                                                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

...为简洁起见,我不会添加所有映射...

当我想打电话给分页器时,我正在接线:

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

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

    __weak typeof(self) weakSelf = self;



        NSString *streamUrl = [NSString stringWithFormat:@"http://api.blahblah.org.au/%@?PageSize=:perPage&Page=:currentPage&X-Requested-With=XMLHttpRequest", streamName];


    if (!self.paginator) {

paginationMapping = [RKObjectMapping mappingForClass:[BBActivityPaginator class]];

        RKResponseDescriptor *activitiesResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[RKObjectMapping mappingForClass:[BBActivityPaginator class]]
                                                                     pathPattern:nil
                                                                         keyPath:nil
                                                                     statusCodes:[NSIndexSet indexSetWithIndex:200]];

        self.paginator = [[BBActivityPaginator alloc]initWithRequest:[NSURLRequest requestWithURL:[[NSURL alloc]initWithString:streamUrl]]
                                                   paginationMapping:paginationMapping
                                                 responseDescriptors:[[NSArray alloc]initWithObjects:activitiesResponseDescriptor, 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);
}];

在这种方法中被激活:

-(void)loadRequest {
    [BBLog Log:@"BBStreamController.loadRequest"];

    self.fetchBatch++;

    [self.paginator loadPage:self.fetchBatch];

    //[self.paginator setPaginatorLoading:YES];
    self.loading = YES;
}

从类初始化:

-(BBStreamController*)initWithGroup:(NSString*)groupIdentifier
                        andDelegate:(id<BBStreamProtocol>)delegate {
    [BBLog Log:@"BBStreamController.initWithGroup:andDelegate:"];

    self = [self init];

    if(self) {
        _controller = delegate;
        groupId = groupIdentifier;

        [self setPaginatorForStream:groupIdentifier];

        [self loadRequest];
    }

    [self loadView];

    return self;
}

除了疼痛什么都没有...

输出:

E restkit:RKPaginator.m:207 Paginator 没有映射信息来计算页数。假设没有页面。2013-05-06 17:48:09.487 BowerBird[26570:5807] W restkit.object_mapping:RKMapperOperation.m:98 添加映射错误:找不到任何属性或关系映射的可映射值

有谁熟悉 RestKit 0.2.x 新分页的细节?

4

1 回答 1

0

您的pathPattern信息keyPath需要在activitiesResponseDescriptor定义上设置,而不是一些实际上不用于任何事情的分页器响应描述符。

响应描述符用于整体响应并描述如何:

  1. 告诉我们有一场比赛:pathPattern
  2. 查找要处理的数据:keyPath
  3. 创建响应对象:数据映射

分页器映射是对此的补充,用于从正在处理的响应中提取页面数据。

将路径模式和关键路径移动到用于请求的响应描述符。从外观上看,路径模式还应该包含“PagedListItems”。

于 2013-05-06T08:32:13.777 回答