【问题标题】:How can i keep value of object same after block completion?块完成后如何保持对象的值相同?
【发布时间】:2017-09-15 06:18:41
【问题描述】:

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

请检查代码:

-(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]) 块完成,我需要先获取单元格。

希望你能解决我的问题。

【问题讨论】:

  • 尝试用__block定义一个变量,
  • __block AttchmentCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  • @AshokPolu 不工作
  • 将前两行保留在您的“startDownload”方法中,因为您在开始下载之前没有执行任何操作,请立即尝试

标签: ios objective-c block


【解决方案1】:

在异步操作之前根据索引路径获取单元格并在异步操作之后对该单元格进行更改是不正确的,因为到那时该索引路径处的单元格可能是不同的单元格,因此您可能会正在更改错误的单元格。

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

【讨论】:

  • 嗨,我的目的是在异步块之前和之后更改..在我需要更改单元格以显示下载开始之前,完成后需要显示下载完成
猜你喜欢
  • 1970-01-01
  • 2019-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多