Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
        }
      }
    }
}
like image 340
amagain Avatar asked Jul 13 '16 09:07

amagain


1 Answers

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

enter image description here

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.

like image 132
Ashish Kakkad Avatar answered Nov 13 '22 03:11

Ashish Kakkad