块完成后如何保持对象的值相同?

时间:2017-09-15 06:18:42

标签: ios objective-c block

我正在使用块来下载文件。我需要在块完成后获取对象的值,它应该与块的开始相同。

请检查代码:

-(void)startDownloadFile:(NSDictionary *)dict withAllFile:(NSMutableArray *)arr withTable:(UITableView *)tableView{

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[arr indexOfObject:dict] inSection:0];
    AttchmentCell *cell = [tableView cellForRowAtIndexPath:indexPath];

   [manager startDownloadCallWithRequestJson:dictDownloadRequest withCompletion:^(NSString *strResponce, NSDictionary *RESPONSE_DATA) {

        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"%@",cell.lblName.text); // cell overites its value of last method
            [cell.downloadButton completeProgressing];

    }];
}

我称这种方法为:

    [self startDownloadFile:arr[0] withAllFile:arr withTable:tableView];
 [self startDownloadFile:arr[1] withAllFile:arr withTable:tableView];

问题:

我总是得到第二块arr [1]的单元格值。

预期:

如果第二次调用(arr [1])块合并,我需要得到第二个单元格。 如果第一次调用(arr [0])块合并,我需要先获取单元格。

希望你能清楚我的问题。

1 个答案:

答案 0 :(得分:0)

在异步操作之前获取基于索引路径的单元并且在异步操作之后对该单元进行更改是不正确的,因为到那时索引路径上的单元可能是不同的单元,因此你可能正在改变错误的单元格。

在异步操作之后(即在调度到主队列的块内),根据索引路径计算单元格。