DEV Community

Discussion on: Swift: Easy way to wait for multiple background tasks to finish

Collapse
 
spicymatt profile image
spicymatt

Well it depends. If your work that is dispatched does not seem to make enough progress, the OS might decide to spawn more threads and you could hit that soft limit and the app could crash. Probably never in your use case where you download a few files. But my use case is a collection of several 10,000 files that need to be accessed for metadata in the background. I used a DispatchGroup to wait for everything to complete before moving to the next step but I was hit hard by thread explosion. Randomly. I moved to NSOperationQueue and added a limit on the number of concurrent operations. There is also a lesser known command called DispatchQueue.performConcurrent(...) that is useful and will limit the number of threads to 8.
This article, albeit a bit old, is good: agostini.tech/2017/08/20/dispatchg...