1

trying to make few requests with DispatchGroup() and getting error:

Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.

My code sample:

DispatchQueue.global().async {
        let dispatchGroup = DispatchGroup()

        dispatchGroup.enter()

        if uploadQueue.contains(.requestA){
            makeUpload(type: . requestA, file: data, completion: {
                dispatchGroup.leave()
            })
        }
 
        dispatchGroup.wait()

        dispatchGroup.enter()

        if uploadQueue.contains(.requestB){
            makeUpload(type: . requestB, file: data, completion: {
                dispatchGroup.leave()
            })
        }
        dispatchGroup.wait()

        dispatchGroup.enter()

        if uploadQueue.contains(.requestC){
            makeUpload(type: . requestC, file: data, completion: {
                dispatchGroup.leave()
            })
        }
        dispatchGroup.wait()
    }
George Heints
  • 1,303
  • 3
  • 20
  • 37
  • I don't know the rest of your code, but I thank `makeUpload` should be called in main queue. – Won Oct 10 '20 at 07:17
  • If you're publishing via combine, you might want to just make a proper upload publisher (e.g. perhaps patterned after the DownloadTaskPublisher example [here](https://stackoverflow.com/a/32322851/1271826)). Use of dispatch group to make asynchronous processes behave synchronously, tying up a worker thread in the process, is probably not a great pattern. And besides, are you 100% sure you want to upload these sequentially (paying a huge performance penalty in the process), or do you just want to make sure that the concurrency is constrained? – Rob Oct 22 '20 at 23:43

0 Answers0