简体   繁体   中英

AFNetworking multiple uploads slowing down main thread

I have a UITabBarController where UITableViewControllerA list files and UITableViewContollerB shows the progress of the files being uploaded.

I have a Singleton class with an upload method that calls my subclass AFHTTPClient and uses NSNotificationCenter to notify my UITableViewControllerB of the upload progress. But this current way is slowing down the UI to where it is almost unusable and I'm not sure how I can improve the process. I read that AFNetworking callback functions are called on the main thread. Is the slow UI response coming from my NSNotificationCenter?

I also would like to mention I'm running this on the Simulator.

Method from my Singleton class.

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

}
}

You are sending to many notifications. Reloading tableview cells is a somewhat expensive operation. I would restrict the posting of notifications to only when a full percent point has changed or to only one every second. You can play around with what works best for you, but 8300 notifications is way to much for the tableview to handle.

Instead of calling reloadRowsAtIndexPaths I changed it to find the cell and update the label that way.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM