【问题标题】:Getting Cell Count after network operation网络运行后获取小区计数
【发布时间】:2018-09-18 18:04:26
【问题描述】:

我正在尝试从网络请求中获取 collectionViewCell 计数,但结果始终为 0(我将 count 变量初始化为该值)我希望视图在从 get 请求中获取计数后加载单元格.我做错了什么我在super.viewDidLoad()之后写了这段代码。

DispatchQueue.global(qos:.background).async {
    let token = "---------------------------"
    let url = URL(string: "https:----------home.json?token="+token)!
    let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
        guard let data = data else { return }
        // print(String(data: data, encoding: .utf8)!)
        let jsonWithObjectRoot = try? JSONSerialization.jsonObject(with: data, options: [])
        //  print(json!)
        if let dictionary = jsonWithObjectRoot as? [String: Any] {
            if let data = dictionary["data"] as? [String:Any]{
                if let posts =  data["posts"] as? [Any]{
                    count = posts.count
                    //print(count) //the value here is 2 
                    for object in posts{

                        if let contentString = object as? [String: Any] {
                            print(contentString["title"] as! String)
                            //   print(contentString["entered"]as! String)
                        }
                    }
                }
            }

        }
    }
    task.resume()
    /* end Request */
    DispatchQueue.main.async{

        self.collectionView.reloadData()
        self.collectionView.collectionViewLayout.invalidateLayout()
    }
}

【问题讨论】:

  • task.resume() 异步运行,因此 reloadData 在您的数据加载之前被触发。

标签: ios swift asynchronous


【解决方案1】:

经典异步案例。您的网络调用是异步的,因此您的重新加载应该在网络调用完成时发生。

DispatchQueue.global(qos:.background).async {
    let token = "---------------------------"
    let url = URL(string: "https:----------home.json?token="+token)!
    let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
        guard let data = data else { return }
        // print(String(data: data, encoding: .utf8)!)
        let jsonWithObjectRoot = try? JSONSerialization.jsonObject(with: data, options: [])
        //  print(json!)
        if let dictionary = jsonWithObjectRoot as? [String: Any] {
            if let data = dictionary["data"] as? [String:Any]{
                if let posts =  data["posts"] as? [Any]{
                    count = posts.count
                    //print(count) //the value here is 2 
                    DispatchQueue.main.async{
                        self.collectionView.reloadData()
                        self.collectionView.collectionViewLayout.invalidateLayout()
                    }
                    for object in posts {
                        if let contentString = object as? [String: Any] {
                            print(contentString["title"] as! String)
                            //   print(contentString["entered"]as! String)
                        }
                    }
                }
            }
        }
    }
    task.resume()
    /* end Request */

}

【讨论】:

  • 我是 gcd 的新手 感谢您的帮助,对我来说非常有效
  • 我又遇到了同样的麻烦,但问题是这次我没有任何收藏视图,我有一个滚动视图,我正在从网络获取图像。我们可以使用什么方法,即在 viewdidload 中获得正确的计数
  • @ssDON_JOE 这似乎是一个新问题。将此作为另一个问题发布,并附上相关代码。
【解决方案2】:

从网络获取data 后,您必须重新加载

count = posts.count
//print(count) //the value here is 2 
DispatchQueue.main.async{
   self.collectionView.reloadData()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多