1

I have a situation where I need to wait for a group of tasks to complete before executing some UI rendering code.

Some of these tasks are network requests which will always complete and leave the dispatch group. Some however, are not network requests and may or may not be completed. In the case where one task is not completed and the calls to leave never balance the calls to enter, the DispatchGroup is never de-allocated.

Is there a way around this using DispatchGroup? Or should I just implement a simple counter myself.

Nick
  • 150
  • 1
  • 12
  • 1
    What kind of networking requests? How do you distinguish between a “long running task” and a “never completing task”? Some code would be helpful. – Martin R Feb 06 '20 at 12:32
  • 2
    In the case of URLRequest you would simply set a timeout. If that fires, the completion handler is called (with an error set) and you can leave the dispatch group. – Martin R Feb 06 '20 at 12:33
  • How are you going to implement a simple counter if some of your tasks wouldn't ever complete? If you are just not going to count such tasks then here is your answer for doing it with DispatchGroup. – Konstantin Oznobihin Feb 06 '20 at 13:35

1 Answers1

1

You should still use DispatchGroup because it's less complex and powerful. The point of a DispatchGroup is to add a dispatchGroup.enter() in every method you execute that you want to finish first before executing a UI update, it is any method like network request, update of your objects and etc. And every time it finishes whether it succeeded or not, you are required to put a dispatchGroup.leave() to maintain the balance and I'm sure that the dispatchGroup.notify will be called.

Sky Shadow
  • 324
  • 2
  • 10