swift - JSON 调用后调度组不会阻塞

标签 swift uitableview grand-central-dispatch alamofire

我试图在填充 TableCell 之前让我的 JSON 调用返回,但是作为 swift 的新手,我无法使用调度队列来阻止调用......任何人都可以告诉我如何让它停止在下面发布的代码中的 group.wait() 行。

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

    let queue:DispatchQueue = DispatchQueue.global()
    let group:DispatchGroup = DispatchGroup()

    Alamofire.request("http://localhost:3000/locations.json").responseJSON { response in
        group.enter()
        //print(response.request)  // original URL request
        //print(response.response) // HTTP URL response
        //print(response.data)     // server data
        //print(response.result)   // result of response serialization
        guard response.result.error == nil else{
            print("Error: unable to get a list of locations.")
            return
        }
        if let JSON = response.result.value {
            //  print("JSON: \(JSON)")
            if let result = response.result.value as? [[String: Any]] {
                nameArray = result.map { ($0["name"] as? String)! }
                print(nameArray[0])

            }
        }
        group.leave()
    }

    group.notify(queue: queue) {
        // This closure will be executed when all tasks are complete
        print("All tasks complete")
    }

    // synchronize on task completion by waiting on the group to block the current thread.
    print("block while waiting on task completion")
    let _ = group.wait()
    print("continue")

    let cell = tableView.dequeueReusableCell(withIdentifier: "Picture", for: indexPath)
    cell.textLabel?.text = LocationData[indexPath.row].name
    print (LocationData[indexPath.row].name)
    print(indexPath.row)
    // cell.textLabel?.text = nameArray[indexPath.row]
    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let vc = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController {
        vc.selectedImage = pictures[indexPath.row]
        navigationController?.pushViewController(vc, animated: true)
    }
}

最佳答案

您必须在执行请求之前调用group.enter(),而不是在闭包内调用。

<小时/>

但是由于 Alamofire 使用主队列作为其完成处理程序,如果您在主队列上等待直到完成处理程序调用 leave(),就会出现死锁。如果您确实想使用此“等待”模式,则必须指定 request 应使用该后台队列作为其完成处理程序。

<小时/>

话虽如此,虽然它在直观上对 wait 很有吸引力,但这并不是它的正确用法。 (使用信号量的其他逻辑上等效的解决方案也不会。)

您应该发起网络请求,然后异步更新单元。但是 cellForRowAtIndexPath 不应该等待网络请求。

<小时/>

我本来打算建议替代实现,但并不完全清楚您要做什么(现在,您正在对表的每一行执行另一个网络请求,这似乎不对)。 LocationData 从哪里来?假设您确实想要执行一个请求来获取位置名称,则可以将该请求移至 viewDidLoad,然后在完成处理程序中调用 tableView.reload() .

var locations: [String]?

override func viewDidLoad() {
    super.viewDidLoad()

    Alamofire.request("http://localhost:3000/locations.json").responseJSON { response in
        guard response.result.error == nil else{
            print("Error: unable to get a list of locations.")
            return
        }
        if let result = response.result.value as? [[String: Any]] {
            self.locations = result.map { $0["name"] as! String }
            self.tableView.reloadData()
        }
    }
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return locations?.count ?? 0
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Picture", for: indexPath)
    cell.textLabel?.text = locations?[indexPath.row]
    return cell
}

关于swift - JSON 调用后调度组不会阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41079780/

相关文章:

objective-c - 获取例程中出现死锁

ios - 同步/异步的行为是否类似于串行/并发,即它们都控制DispatchQueues还是仅执行同步/异步控制线程

swift - 使用通知中心将值设置为 TableView 中的标签

ios - SwiftUI Spacer 无法在 HStack 内的 Toggle 旁边工作

arrays - 快速交换数组对象

ios - Monotouch UISwitch.valueChanged 委托(delegate)目标错误

iphone - 使用 CGLayer 缓存 UITableView 中的单元格

ios - 自定义表格标题部分

ios - 继续的 UIButton

objective-c - 学习 block 的建议资源