- swift:async + JSON + completion + DispatchGroup

问题描述 投票:0回答:2

我的viewcontroller-class中的代码在JSON-download-process准备好之前执行,即使func中有一个完成处理程序用于下载JSON和DispatchGroup()。我将JSON数据存储在名为“fetchedModules”的数组中,在这种情况下,这将填充11个项目。为什么会这样?

result in console:
---> in Class PostCell - func numberOfSections: 0
JSON call finished 

// ViewController
override func viewDidLoad() {
    super.viewDidLoad()

    let group = DispatchGroup()

    group.enter()

    self.fetchJSON()
    // here calling downloadJSONasync

    group.leave()

    group.notify(queue: .main)  {
        print("JSON call finished")
    }

    ...

    // networkService with completion
    func downloadJSONasync(searchItem: String, completion: @escaping ([NSDictionary]) -> Void) {

    //request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData

    request.httpMethod = "GET"

    let configuration = URLSessionConfiguration.default
    //let session = URLSession(configuration: configuration, delegate: nil)

    let session = URLSession(configuration: configuration)

    let task = session.dataTask(with: request, completionHandler: {(data, response, error) in

        guard let data = data, error == nil else { return }

        if (error != nil) {
            print("error!")
        }

        else{

            do {

                let fetchedData = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! [NSDictionary]

                completion(fetchedData)

            }

            catch {
                print("error")
            }

        }

    })

    task.resume()

}

// call in viewController
override func numberOfSections(in tableView: UITableView) -> Int {

    print("---> in Class PostCell - func numberOfSections: \(String(describing: fetchedModules.count))")
    return fetchedModules.count

// code of fetchJSON
func fetchJSON()
{

    let baseurl = AppConstants.Domains.baseurl // https://m.myapp2go.de

    let compositURL = baseurl + "getmodules_noItems.php?id=\(AppConstants.appString.startString)"

    let encodedUrl : String! = compositURL.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) // remove the spaces in the url string for safty reason

    let JSONurl = URL(string: encodedUrl)! // convert the string into url

    var JSONrequest = URLRequest(url: JSONurl) // make request

    JSONrequest.httpMethod = "GET"

    //JSONrequest.cachePolicy = .reloadIgnoringCacheData

    let networkService = NetworkService(request: JSONrequest)

    networkService.downloadJSONasync(searchItem: AppConstants.appString.startString, completion: { (fetchedData) in

        fetchedModules.removeAll()

        DispatchQueue.main.async {

            for eachFetchedModul in fetchedData {

                let eachModul = eachFetchedModul

                if

                    let custid    = eachModul["custid"]    as? String,
                    let modulcat  = eachModul["modulcat"]  as? String,
                    let modulname = eachModul["modulname"] as? String,
                    let comment   = eachModul["comment"]   as? String

                {

                    fetchedModules.append(CModules(custid: custid, modulcat: modulcat, modulname: modulname, comment: comment))

                    print(custid)
                    print(modulcat)
                    print(modulname)
                    print(comment)

                    print("---------------------")
                }

            }// for end

            // ... somehow set data source array for your table view
            self.tableView.reloadData()
        }// dispatch



    }


)} // func end
json swift asynchronous dispatch-async completion
2个回答
1
投票

因为fetchJSON会在下载JSON之前立即返回。效果是DispatchGroup被引入并立即离开,而不是等待JSON:

group.enter()
self.fetchJSON() // returns immediately
group.leave()    // the JSON has yet to be downloaded

要等到JSON到达,请向fetchJSON添加完成处理程序:

override func viewDidLoad() {
    group.enter()
    self.fetchJSON { 
        group.notify(queue: .main)  {
            print("JSON call finished")
        }
        group.leave()
    }
}

// Change the data type of the completion handler accordingly
func fetchJSON(completionHandler: @escaping (Data?) -> Void) {
    // ...
    networkService.downloadJSONasync(searchItem: AppConstants.appString.startString) { fetchedData in
        defer { completionHandler(fetchedData) }
        // ...
    }
)

使用defer可确保始终调用完成处理程序,无论外部闭包如何返回。我不清楚为什么你在这里使用DispatchGroup,因为没有等待,但我保留它来回答你的问题。


1
投票

您的表视图从一开始就没有任何数据,因为尚未提取任何数据。所以没关系,表视图没有单元格。您只需要对表视图进行reloadData,因为现在您已将元素附加到表视图的数据源数组,现在您应该显示此数据。


请不要为此使用DispatchGroup,只需使用你的方法的completion参数和收到数据后的completion闭包内设置表视图的数据源数组然后...重新加载表视图的数据

downloadJSONasync(searchItem: "someString") { dictionary in
    DispatchQueue.main.async { // don't forget that your code doesn't run on the main thread
        // ... somehow set data source array for your table view
        self.tableView.reloadData()
    }
}

请注意,您应该避免使用NSDictonary,而应该使用Dictionary。同样来自Swift 4+,您可以使用Codable而不是JSONSerialization

© www.soinside.com 2019 - 2024. All rights reserved.