AFHTTPSessionManager加载第一次请求缓慢

时间:2018-01-19 01:47:58

标签: ios objective-c uitableview afhttpsessionmanager

我在tableview中应用了这段代码。但是,我意识到第一次请求数据加载速度会变慢。之后,如果我访问同一页面,记录将加载更快。有没有想过第一次加载?谢谢你的帮助。

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

[manager GET:@"http://api.domainname.com/api/abc" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {

    _serverDataArr = responseObject;
    self.dataArr=[NSMutableArray array];

    for (NSDictionary *subDic in self.serverDataArr) {

        Friend_Model *model=[[Friend_Model alloc]initWithDic:subDic];
        [self.dataArr addObject:model];
    }

    _rowArr=[Friend_DataHelper getFriendListDataBy:self.dataArr];
    _sectionArr=[Friend_DataHelper getFriendListSectionBy:[_rowArr mutableCopy]];

    [self.tableView reloadData];


} failure:^(NSURLSessionTask *operation, NSError *error) {

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Error Retrieving Ninjas"
                                                                     message:[error localizedDescription]
                                                              preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok"
                                                       style:UIAlertActionStyleCancel
                                                     handler:nil];

    [alertVC addAction:okAction];

    [self presentViewController:alertVC animated:YES completion:nil];
}];

这是我的cellForRowAtIndexPath代码: -

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIde=@"cellIde";
    Friend_TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIde];
    if (cell==nil) {
        cell=[[Friend_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIde];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    if (tableView==_searchDisplayController.searchResultsTableView){
        NSString *url_Img_FULL = [_searchResultArr[indexPath.row] valueForKey:@"image"];
        [cell.headImageView setImageWithURL:[NSURL URLWithString:url_Img_FULL] placeholderImage:[UIImage imageNamed:@"Placeholder.png"]];
        [cell.nameLabel setText:[_searchResultArr[indexPath.row] valueForKey:@"name"]];
    }else{
        Friend_Model *model=_rowArr[indexPath.section][indexPath.row];

        [cell.headImageView setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"Placeholder.png"]];

        [cell.nameLabel setText:model.name];
    }

    return cell;
}

1 个答案:

答案 0 :(得分:1)

看起来你正在做完全合适的事情来形成请求并呈现结果。您可能会看到第1个和第n个请求之间存在很大差异,因为您的后端处于唤醒状态,并且数据已缓存在堆栈的各个点上。除了提前移动请求之外,很难在此处提出更多建议,可能是应用启动,用户可能没有注意到延迟。

另一个想法是根据表滚动的位置更懒惰地请求表行的数据。当用户在表格底部进行拉动时,显示忙碌指示符并获取更多数据。

相关问题