user9358473
user9358473

Reputation:

How to know when 2 parallel threads request is completed in iOS

for eg. I have two separate requests and both running in different threads then how do I get to know that both are done with their request and completed.

Upvotes: 0

Views: 50

Answers (2)

Pankaj Bhardwaj
Pankaj Bhardwaj

Reputation: 2131

See below implementation , This can help you in detail:

let queue = DispatchQueue(label: "com.learn.swift", attributes: .concurrent, target: .main)
let group = DispatchGroup()

group.enter()
queue.async (group: group) {
    print("1st Thread")
    group.leave()
}

group.enter()
queue.async (group: group) {
    print("2nd Thread")
    group.leave()
}
group.notify(queue: DispatchQueue.main) {
    print("All done")
}

Output:

1st Thread

2nd Thread

All done

Upvotes: 2

Rahul Sharma
Rahul Sharma

Reputation: 36

You can use dispatch_group here. Enter into group using dispatch_enter and leave by using dispatch_leave. Both can notify by dispatch_group_notify

Upvotes: 1

Related Questions