RestKit分页

时间:2013-05-06 06:34:15

标签: ios restkit

我遇到了分页问题。

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

API Json response

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

/ * 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)]];

与此映射有关:

/ * BBActivity * /

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新分页的细节吗?

1 个答案:

答案 0 :(得分:0)

您的pathPatternkeyPath信息需要在activitiesResponseDescriptor定义上设置,而不是某些实际上没有用于任何事情的paginator响应描述符。

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

  1. 告诉我们匹配:pathPattern
  2. 查找要处理的数据:keyPath
  3. 创建响应对象:数据映射
  4. paginator映射是此附加的,用于从正在处理的响应中提取页面数据。

    将路径模式和密钥路径移动到用于请求的响应描述符。路径模式还应包含“PagedListItems”的外观。