How to listen to Published outside of SwiftUI view

Issue #782

Use $ to access Publisher

1
2
3
final class Store: ObservableObject {
@Published var showsSideWindow: Bool = false
}
1
2
3
4
5
6
7
8
9
10
var anyCancellables = Set<AnyCancellable>()

store.$showsSideWindow
.removeDuplicates()
.throttle(for: 0.2, scheduler: RunLoop.main, latest: true)
.receive(on: RunLoop.main)
.sink(receiveValue: { shows in
preferenceManager.reloadPosition(shows: shows)
})
.store(in: &anyCancellables)

Updated at 2021-02-25 21:56:42

Comments