如何使用Swift 3中的DispatchQueue.main.async / Block / Closures更新数据模型

时间:2017-05-02 18:49:20

标签: swift swift3 closures capture-list

我正在学习斯威夫特。我有一个问题。

问题 - 我有带图片网址的DataModel,所以第一次会从网址下载图片,当然第二次会赢得#t。因此,当我在块中获取图像时,我想用图像更新我的数据模型。但它不起作用。

  • 我尝试了inout功能
  • 还完成了汉德回电

ViewController.swift

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "OffersTableCell", for: indexPath) as! OffersTableCell

    var model = offersArray[indexPath.row] as! OffersModel
    cell.updateOfferCellWith(model: model, completionHandler: { image in
        model.image = image
    })///// I am getting image here, but model is not getting update
    return cell
}

Cell.Swift

func updateOfferCellWith(model: OffersModel, completionHandler:@escaping (UIImage) -> Void) {

    if (model.image != nil) { //Second time this block should be execute, but model image is always nil
        DispatchQueue.main.async { [weak self] in
            self?.offerImageView.image = model.image
        }
    }
    else{
        //First time- Download image from URL
        ImageDownloader.downloadImage(imageUrl: model.offerImageURL) { [weak self] image in
            DispatchQueue.main.async {[model] in
                var model = model
                self?.offerImageView.image = image
                //model.image = image*//This is also not working*
                completionHandler(image)*//Call back*
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

下面是一个基于你的代码的潜在解决方案,但是,正如vadain在评论中所说,有点危险,因为图像加载是异步的,并且可能在单元格不再存在时返回图像。

理想情况下,您要做的是以持久的方式设置模型,并发送异步请求以下载它的图像。然后,当图像返回时,它会更新模型,模型会广播一条消息,说明它的图像已经更新。当模型存在时,单元可以监听并响应模型的更新。

func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "OffersTableCell", for: indexPath) as! OffersTableCell

    var model = offersArray[indexPath.row] as! OffersModel

    if image = model.image {
        // setting directly here... you could call an image on the cell if you want more data encapuslation
        cell.offerImageView.image = model.image
    } else {
        ImageDownloader.downloadImage(imageUrl: model.offerImageURL) { [weak self] image in
            DispatchQueue.main.async {
                model.image = image
                cell.offerImageView.image = image
            }
        }
    }

    return cell
}