I'm not very much old with DispatchQueue things (including DispatchGroups, Qos etc.). And I have a concern right now.
It's like, I have a ViewController that is reused for Android's ViewPager UI. And in that VC, I have a TableView which reloads every time the "page" in the ViewPager is changed.
I have a favourite button property in the TableView cell. When user tap on that button, it stores required information for a post call (JSON).
I want to make the post call whenever viewWillDisappear runs. And there I want to create background threads for each "page" and catch the already created thread in case a user hits a "page" for which the post call could not culminate for any reasons.
Here is what I have in my class:
var workItem1: DispatchWorkItem!
var dispatch1: DispatchQueue!
and
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}
What should I do to achieve my goal?
You can theoretically create as many queues as you need. You can also choose between a serial queue (tasks are being worked on after each other) or a concurrent queue (tasks are being worked on concurrently)
let concurrentQueue = DispatchQueue(label: "my_queue_1", attributes: .concurrent)
concurrentQueue.sync {
//do your thing
}
BUT: It's bad practice to create a queue for each call. Thats not what queues are for. What you should do instead is use a concurrent queue where every task represents a POST-call.
Or even better you should ask yourself if you can't just submit an array of all the pages the user has favourited to the server in one go. This would eliminate the need for a queue somewhat.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With