'How to update UI outside the main thread, from OperationsQueue in Swift?

Hello guys

I face a problem with updating SwiftUI view using Combine when I implement OperationsQueue which uses concurrent threads. Adding operations to the queue causes every operation execute on non-main thread, thereby whole changes I do with Operation's publishers, even if I call .receive(on: DispatchQueue.main) doesn't work. To see changes I need to manually reset view by f.e. change to another and back.

Workarounds like:

 DispatchQueue.main.async {
                    self.topImageView.image = nil
                }

&

public class ViewModelSample : ObservableObject
    func updateView(){
        self.objectWillChange.send()
    }
}

are not satisfactional for me.


I tried to make publisher in Operation child class, and force receiving on main thread, but it was a bad idea, still not working. $status is stored @Published var status: OperationStatus = .notStarted. Case below:

  var statusPublisher: AnyPublisher<OperationStatus, Never> {
        $status
            .receive(on: DispatchQueue.main)
            .eraseToAnyPublisher()
    }

What is a good answer?

Thanks for any help! Have a nice day! :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source