首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从多个api异步加载数据

从多个api异步加载数据
EN

Stack Overflow用户
提问于 2015-09-11 13:27:05
回答 1查看 733关注 0票数 0

从一个api请求加载数据并将其存储在一个数组中(假设n个对象进入响应json对象),另一个api请求从第一个api请求中获取参数并加载n个对象的状态。

1)第一个api请求将加载n个对象并将其显示在表中:

代码语言:javascript
运行
复制
dispatch_queue_t loadDataQueue = dispatch_queue_create("loadDataQueue",NULL);
dispatch_async(loadDataQueue, ^{      
    // Perform long running process
    [self loadData];
    dispatch_async(dispatch_get_main_queue(), ^{
        // Update the UI
        [tableView reloadData];
        [self hideActivityView];
    });
});

2)现在调用loadstatus方法,它从objectatindex中获取参数并加载objectatindex的状态数据。因此,这个方法在cellForRowAtIndexPath方法中调用n次。

代码语言:javascript
运行
复制
dispatch_queue_t loadStatusQueue = dispatch_queue_create("loadStatusQueue",NULL);
dispatch_async(loadStatusQueue, ^{     
    // Perform long running process
    [self loadStatus];
    dispatch_async(dispatch_get_main_queue(), ^
        // Update the UI
        [tableView reloadData];
    });
});

它一次更新一行。所以重装表格n次。加载所有对象的状态需要时间。出现了一些吊死问题。

有人能为这个或其他方法提供有效的解决方案吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-11 16:57:13

你问题中的信息有点不清楚(特别是关于objectAtIndex.这是否意味着在cellForRowAtIndexPath中您获得了单元格的信息,然后根据它发送了另一个异步请求来获取它的状态?)

这可能不足以说明所有的原因可能会使您的应用程序缓慢,但我可以说,这不是一个好主意,重新加载整个表,只更新一个单元格。另外,我认为您应该先调用loadData来获取基本信息的完整数据列表,然后调用loadStatus表示“可见”单元格。

我想您知道如何将加载的数据/状态存储在数组中,以防止重取数据。因此,下面的示例可能是您可以采用的改进性能的方法:

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ReusableCell" forIndexPath:indexPath];

    [cell configureData:self.loadedData[indexPath.row]];

    if (self.loadedStatus[indexPath.row]) {
        // If the status has been loaded then
        [cell configureStatus:self.loadedStatus[indexPath.row]];
    } else {
        dispatch_queue_t loadStatusQueue = dispatch_queue_create("loadStatusQueue",NULL);

        __weak __typeof(self) weakSelf = self;
        dispatch_async(loadStatusQueue, ^{
            __strong __typeof(weakSelf) strongSelf = weakSelf;
            if (!strongSelf) {
                return;
            }

            // Perform your long running process here
            // Eg: [strongSelf loadStatusForIndex:indexPath.row];

            UITableViewCell *blockCell = (UITableViewCell *)[strongSelf.tableView cellForRowAtIndexPath:indexPath];

            dispatch_async(dispatch_get_main_queue(), ^{
                [strongSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            });
        }
                       });
        ....
    }

    return cell;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32524660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档