简体   繁体   中英

dispatch_group_notify has been replaced by instance method DispatchGroup.notify(qos:flags:queue:execute:)

I am trying to migrate my swift2.2 codebase to swift3 . Beforehand, for GCD , this code used to work for me. But it shows an error of dispatch_group_notify has been replaced by instance method DispatchGroup.notify(qos:flags:queue:execute:) How can I tackle this error?

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

This is the new code I am writing and I am stuck while I am leaving the group.

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)
        }
      }
    }
}

I think you have combine old and new both way...

在此输入图像描述

dispatch_group_notify is old code.

Try as follows :

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

}

Through the variable you can also use like as :

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

}

And also as compiler displayed you can use as follows :

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

}       

Hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM