Please consider this code:
import Combine
class TestViewModel: ObservableObject {
@Published public var value: Int?
var cancellable: AnyCancellable?
init() {
cancellable = Just(1)
.receive(on: DispatchQueue.main)
.assign(to: \.value, on: self)
}
}
As is when TestViewModel is created on the main thread self.value
isn't assigned to until sometime after the init function exits. This means SwiftUI does two draws one for value == nil
and one for value == 1
.
Is there a way to make receive(on:)
deliver the value immediately when called from the same scheduler (DispatchQueue.main in this case) instead of having it queued?