2013-08-07 42 views
1

我有一个UITabBarController其中UITableViewControllerA列表文件和UITableViewContollerB显示正在上传的文件的进度。AFNetworking多个上传减慢主线程

我有一个Singleton类的上传方法,调用我的子类AFHTTPClient并使用NSNotificationCenter来通知我的UITableViewControllerB的上传进度。但目前的这种方式正在减慢用户界面到几乎无法使用的地步,我不知道如何改进这一过程。我读过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]; 

} 
} 
+0

您同时运行多少个上传?使用仪器来查看发生了什么。 – Wain

+0

@我当时正在运行2上传30MB +。我应该使用哪种仪器? –

+0

使用范围,检查处理器使用情况(时间分析)和内存使用情况。 2同时上传应该没问题。您获得了多少进度更新。 – Wain

回答

0

而不是调用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; 
     } 
    } 
1

要发送到很多通知。重新加载tableview单元是一个相当昂贵的操作。只有在全部百分点发生变化或者每秒只有一次时,我才会限制发布通知。 你可以玩最适合你的东西,但8300通知是很多的方式来处理tableview。

+0

当我在我的计算机上时,我会给出一个镜头。谢谢karlofk! –

+0

如果这个答案是正确的,请这样标记。谢谢! – karlofk