I have LocationManager class that works well. This is part of LocationManager class.
var headingDegree = PassthroughSubject<Double, Never>()
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
headingDegree.send(-newHeading.magneticHeading)
}
headingDegree
is the value I want to send to my ViewModel. I debugged it to make sure it has correct values, and it does.
So, in my view model,
class CompassViewViewModel: ObservableObject {
@Published var degree: Double = 0.0
@State var cancellable = Set<AnyCancellable>()
func update() {
LocationManager.shared.headingDegree.sink {
self.degree = $0
}
.store(in: &cancellable)
}
I sinked my headingDegree
. This is the part that brings my problems. If I put breakpoints in update()
function, self.degree = $0
is never called.
This is how my View looks like.
struct CompassView: View {
@ObservedObject var viewModel: CompassViewViewModel
init(viewModel: CompassViewViewModel) {
self.viewModel = viewModel
}
var body: some View {
ZStack {
Text("aa")
.font(.footnote)
}
.onAppear {
self.viewModel.update()
}
}
}
Could you tell me why my sink() is not called?
I changed @State var cancellable = Set<AnyCancellable>()
to private var cancellable = Set<AnyCancellable>()
and it works well.