dispatch_group_notify已被实例方法DispatchGroup.notify取代(qos:flags:queue:execute :)

时间:2016-07-13 09:14:45

标签: ios swift swift3 ios10 grand-central-dispatch

我正在尝试将swift2.2代码库迁移到swift3。事先,对于GCD,此代码曾经适合我。但它显示错误为dispatch_group_notify has been replaced by instance method DispatchGroup.notify(qos:flags:queue:execute:) 我该如何处理这个错误?

   dispatch_group_notify(group, dispatch_get_main_queue()) {
        if productsError != nil || citiesError != nil || usersError != nil {
            completionHandler(false)
        } else {
            completionHandler(true)
        }
    }

这是我写的新代码,当我离开小组时,我被困住了。

class APIHandler {

func requestDataFromTheServerWithCompletionhandler(completionHandler: ((Bool) -> Void)) {

    let group = DispatchGroup()
    var productsError: NSError?
    var citiesError: NSError?
    var usersError: NSError?
    var categoriesError: NSError?

    let manager = SessionManager()

    // MARK:- Products
    group.enter()
    let productsParser = ProductsParser()
    let productsURL = URLs.productsURL

    manager.requestDataWithCompetionHandler(urlString: productsURL) {( responseData, error) in
        if responseData != nil {
            print("Products Success")
            productsParser.parseAndStoreProductsData(dataToParse: responseData!)
        }
        else {
            print("Products Error")
            productsError = error
        }
        group.leave()
    }

    dispatch_group_notify(group, DispatchQueue.main) {
        if productsError != nil || citiesError != nil || usersError != nil {
            completionHandler(false)
        } else {
            completionHandler(true)
        }
      }
    }
}

1 个答案:

答案 0 :(得分:11)

我认为你既有新旧结合......

enter image description here

dispatch_group_notify是旧代码。

尝试如下:

DispatchGroup().notify(queue: DispatchQueue.main) { 

}

通过变量,你也可以使用如:

let dispatchGroup = DispatchGroup()
dispatchGroup.notify(queue: DispatchQueue.main) {

}

并且当显示编译器时,您可以使用如下:

dispatchGroup.notify(qos: DispatchQoS.background, flags: DispatchWorkItemFlags.assignCurrentContext, queue: DispatchQueue.main) { 

}       

希望它对你有所帮助。