iosobjective-cuitableviewafhttpsessionmanager

AFHTTPSessionManager load slow for first time request


I applied this code in my tableview. However, i realise that data will load slow for first time request. After that, if i access to the same page, the records will be loaded faster. Any idea to improve for first time loading? Thanks for your help.

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];
}];

And here is my cellForRowAtIndexPath code :-

-(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;
}

Solution

  • It looks like you're doing perfectly appropriate stuff to form the request and present the results. You might be seeing a big difference between the 1st and nth request because your back-end is awake, and data has been cached at various points on the stack. Hard to suggest much here besides moving the request earlier, maybe to app startup, where the user might not notice as much delay.

    Another idea would be to request data for table rows more lazily, based on where the table is scrolled. When user gestures a pull at the bottom of the table, show a busy indicator and go get more data.