ios - 网络运行后获取小区计数

标签 ios swift asynchronous

我试图从网络请求中获取 collectionViewCell 计数,但该值始终为 0(我初始化了计数变量)我希望 View 在从 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()
    }
}

最佳答案

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

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 */

}

关于ios - 网络运行后获取小区计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52392288/

相关文章:

swift - 单击 tvos 应用程序中 TableView 单元格内的 Collection View 单元格

ios - 重启后用户 phoneNumber 属性为 nil

ios - 在swift 4中更改自动打印数字的某个字符串

ios - [NSDictionaryI setObject :forKey:]: unrecognized selector sent to instance

ios - UITableView 部分标题与 64 位 iPad 上的行重叠。在 32 位上工作正常

Java CompletableFuture : only first result

c# - 使用 ToList 的异步、等待和任务

iphone - 使用 Objective-C iOS 以编程方式创建 TableVIew

Swift:如何使我的自定义结构 Node<T> 符合 Hashable?

asp.net-mvc-3 - ASP.NET MVC 3 和 jQuery 验证