【问题标题】:AFNetworking multiple uploads slowing down main threadAFNetworking 多次上传减慢主线程
【发布时间】:2013-08-07 06:09:21
【问题描述】:

我有一个UITabBarController,其中 UITableViewControllerA 列出文件,UITableViewContollerB 显示正在上传的文件的进度。

我有一个单例类,其上传方法调用我的子类AFHTTPClient 并使用NSNotificationCenter 通知我的 UITableViewControllerB 上传进度。但是这种当前的方式正在将 UI 减慢到几乎无法使用的程度,我不确定如何改进这个过程。我读到 AFNetworking 回调函数是在主线程上调用的。缓慢的 UI 响应是否来自我的 NSNotificationCenter?

我还想提一下我正在模拟器上运行它。

我的 Singleton 类中的方法。

 NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:uniqueName forKey:@"unique"];
[dict setObject:[NSNumber numberWithFloat:0] forKey:@"progress"];

[self.active addObject:dict];

[[CustomHTTP client] uploadFileName:@"filename" withBytes:data toPath:serverPath progress:^(float progress) {
    [dict setObject:progress forKey:@"progress"];

    NSMutableDictionary *info = [[NSMutableDictionary alloc] init];
    [info setObject:[NSNumber numberWithInt:[self getIndexByUniquename:uniqueName]] forKey:@"row"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ProgressNotification" object:self userInfo:info];

} success:^(AFHTTPRequestOperation *operation, id responseObject) {

} andFailure:^(AFHTTPRequestOperation *operation, NSError *error) {

}];

UITableViewControllerB.m

- (void) receiveTestNotification:(NSNotification *) notification    {

if ([[notification name] isEqualToString:@"ProgressNotification"]) {
    NSDictionary *dict = notification.userInfo;

    int row = [[dict objectForKey:@"row"] intValue];

    self.inProgress = [Transfer defaultTransfer].active;

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                          withRowAnimation:UITableViewRowAnimationNone];

}
}

【问题讨论】:

  • 您同时运行多少次上传?使用仪器查看正在发生的事情。
  • @Wain 我正在运行 2 次上传 30MB+。我应该使用哪种仪器?
  • 使用范围,检查处理器使用情况(时间分析)和内存使用情况。同时上传2个应该没问题。您获得了多少进度更新。
  • @Wain 获取我的 34MB 文件,8300 个进度更新。
  • 仅在特定时间段进行更新,例如每 0.2 秒更新一次 UI 应该没问题、流畅且仍能响应

标签: iphone ios afnetworking


【解决方案1】:

您正在发送许多通知。重新加载 tableview 单元格是一个有点昂贵的操作。我会将通知的发布限制为仅在一个完整的百分点发生变化时或每秒仅发布一个。 您可以尝试最适合您的方法,但 8300 条通知对于 tableview 来说已经足够处理了。

【讨论】:

  • 当我在电脑旁时,我会试一试。谢谢卡洛夫克!
【解决方案2】:

我没有调用reloadRowsAtIndexPaths,而是将其更改为查找单元格并以这种方式更新标签。

    for (int i = 0; i < [self.items count]; i++) {
        if ([self.items objectAtIndex:i] == transfer) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            TransferCell *cell = (TransferCell *)[self.tableView cellForRowAtIndexPath:indexPath];
            cell.percentLabel.text = transfer.completed;
            break;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多