[英]sink() is not called in combine swift
I have LocationManager class that works well.我有一个运行良好的 LocationManager 类。 This is part of LocationManager class.
这是 LocationManager 类的一部分。
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. headingDegree
是我想发送到我的 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
.我
headingDegree
。 This is the part that brings my problems.这是给我带来问题的部分。 If I put breakpoints in
update()
function, self.degree = $0
is never called.如果我在
update()
函数中放置断点,则永远不会调用self.degree = $0
。
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?你能告诉我为什么我的 sink() 没有被调用吗?
我将@State var cancellable = Set<AnyCancellable>()
更改为private var cancellable = Set<AnyCancellable>()
并且效果很好。
in swiftUI1, viewDidLoad in UIHostingcontroller isn't called.在 swiftUI1 中,不会调用 UIHostingcontroller 中的 viewDidLoad。
when using combine, with UIKit, subscribing (Sink) in viewDidload, didn't work, instead, subscribe in viewDidAppear当使用结合使用 UIKit 时,在 viewDidload 中订阅 (Sink) 不起作用,而是在 viewDidAppear 中订阅
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.