objective-c分页请求一般解决办法

- (void)searchUserListDataWithPull:(BOOL)isPull atBegin:(void(^)(void))begin atComplateHandle:(void(^)(void))complateHandle
{
    if(_isRequest == YES) {
        [self.refreshManager endRefreshWithType:HHRefreshTypeHeader];
        [self.refreshManager endRefreshWithType:HHRefreshTypeFooter];
        return;
    }
    if(isPull == NO){
        double currentRequestTime = [[NSDate date] timeIntervalSince1970];
        if(currentRequestTime - _firstRequesttamp < RequestRefreshControlTime){
            [self.refreshManager endRefreshWithType:HHRefreshTypeHeader];
            [[PublicTopAlertView initTopAlertViewWithWindow] setTopAlertString:@"操作太频繁了!" atStayTime:1.5 atSelectedHandle:nil];
            return;
        }else{
            _firstRequesttamp = currentRequestTime;
        }
    }
    if(_isRequestEnd == YES && isPull == YES){
        [self.refreshManager endRefreshWithType:HHRefreshTypeFooter];
        [[PublicTopAlertView initTopAlertViewWithWindow] setTopAlertString:@"没有那么多伙伴了!" atStayTime:1.5 atSelectedHandle:nil];
        return;
    };
    _isRequest = YES;
    NSInteger currentPageNum = YES == isPull ? _currentPageNum : 1;
    NSInteger tempPageNum = _currentPageNum == 1 ? _currentPageNum : _currentPageNum - 1;
    NSInteger listDefaultNubmer = YES == isPull ? RequestListDefaultNubmer : tempPageNum * RequestListDefaultNubmer;
    NSDictionary *params = @{@"currentPage":@(currentPageNum),@"pageSize":@(listDefaultNubmer)};
    [self IM_SearchPage_Latest_User_ListInfo:params atWillRequestHandle:begin atComplateHandle:^(NSError *error, IMSuperModel *superModel) {
        _isRequest = NO;
        if(complateHandle != nil) complateHandle();
        if(superModel.code == RequestSucceedCode){
            NSMutableArray *tempDatasource = [IMSearchUserModel getAllDataByFormatWithResponseObjectArray:superModel.data];
            if(NO == isPull){
                if([tempDatasource count] >= RequestListDefaultNubmer){
                    if(self.datasource != nil && self.datasource.count > 0){
                        if(self.datasource.count > RequestListDefaultNubmer){
                            NSInteger divisor = self.datasource.count / RequestListDefaultNubmer;
                            _currentPageNum = divisor + 1;
                        }
                    }else{
                        _currentPageNum++;
                    }
                }else{
                    _isRequestEnd = YES;
                }
                self.datasource = tempDatasource;
            }else{
                if(tempDatasource != nil && tempDatasource.count > 0){
                    if([tempDatasource count] < RequestListDefaultNubmer){
                        _isRequestEnd = YES;
                    }else{
                        _currentPageNum++;
                    }
                    if(self.datasource != nil)
                        [self.datasource addObjectsFromArray:tempDatasource];
                    else
                        self.datasource = tempDatasource;
                }else{
                    _isRequestEnd = YES;
                }
            }
            if(self.datasource == nil || self.datasource.count == 0){
                [self.listCollectionView showNoDataViewDefaultImageWithText:@"还没有数据呢"];
            }else{
                [self.listCollectionView hiddenNoDataView];
                WeakSelf(weakSelf);
                [self.listCollectionView performBatchUpdates:^{
                    [weakSelf.listCollectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
                } completion:nil];
            }
        }else{
            [[PublicTopAlertView initTopAlertViewWithWindow] setTopAlertString:@"请求出错,请再次尝试!" atStayTime:1.5 atSelectedHandle:nil];
            if(self.datasource == nil)
                [self.listCollectionView showNoDataViewDefaultImageWithText:@"出错啦,请重新请求"];
        }
    }];
}

Leave a Reply

Required fields are marked *