Ashley Mills
Ashley Mills

Reputation: 53231

Correct way to perform async operations sequentially

I need to perform an async operation for each element in an array, one at at time. This operation calls back on the main queue.

func fetchResults(for: array, completion: () -> Void) {

    var results: [OtherObject]: []
    let queue = DispatchQueue(label: "Serial Queue")
    queue.sync {

        let group = DispatchGroup()
        for object in array {

            group.enter()
            WebService().fetch(for: object) { result in
                // Calls back on main queue
                // Handle result
                results.append(something)

                group.leave()
            }
            group.wait()
        }
    }

    print(results) // Never reached
    completion()
}

The WebService call isn't calling back - which I think is telling me the main queue is blocked, but I can't understand why.

Upvotes: 3

Views: 2703

Answers (3)

David Pasztor
David Pasztor

Reputation: 54805

You should use group.notify() rather than group.wait(), since the latter is a synchronous, blocking operation.

I also don't see a point of dispatching to a queue if you only dispatch a single work item once.

func fetchResults(for: array, completion: () -> Void) {

    var results: [OtherObject]: []
    let group = DispatchGroup()
    for object in array {
        group.enter()
        WebService().fetch(for: object) { result in
            // Calls back on main queue
            // Handle result
            results.append(something)

            group.leave()
        }
    }

    group.notify(queue: DispatchQueue.main) {
        print(results)
        completion()
    }
}

Upvotes: 4

vadian
vadian

Reputation: 285290

Maybe it's just a typo but basically don't run the queue synchronously.

Then instead of wait use notify outside(!) of the loop and print the results within the queue.

queue.async {

    let group = DispatchGroup()
    for object in array {

        group.enter()
        WebService().fetch(for: object) { result in
            // Calls back on main queue

            // Handle result
            results.append(something)

            group.leave()
        }
    }
    group.notify(queue: DispatchQueue.main) {
        print(results)
        completion()
    }
}

Upvotes: 2

Crocobag
Crocobag

Reputation: 2340

I d'ont think your main queue is locked, otherwise you would probably have an infinite loading on your app, as if it crashed ( in MacOS that's for sure ).

Here is what worked for me, maybe it will help :

class func synchronize(completion: @escaping (_ error: Bool) -> Void) {

    DispatchQueue.global(qos: .background).async {

        // Background Thread
        var error = false
        let group = DispatchGroup()
        synchronizeObject1(group: group){ error = true }
        synchronizeObject2(group: group){ error = true }
        synchronizeObject3(group: group){ error = true }
        group.wait() // will wait for everyone to sync

        DispatchQueue.main.async {
            // Run UI Updates or call completion block
            completion(error)
        }
    }
}




class func synchronizeObject1(group: DispatchGroup, errorHandler: @escaping () -> Void){

    group.enter()
    WebservicesController.shared.getAllObjects1() { _ in

        // Do My stuff

        // Note: if an error occures I call errorHandler()

        group.leave()
    }
}

If I would say, it may come from the queue.sync instead of queue.async. But I'm not an expert on Asynchronous calls.

Hope it helps

Upvotes: 0

Related Questions