已发布的对象未更新
我正在构建Watchos Companion应用程序。从视图中,有一个读写和写入价值的滑块。 iOS和WatchOS应用程序之间正在传达此值。
struct ReadingView: View {
@EnvironmentObject var watcher: ViewModelWatch
var body: some View {
CompactSlider(value: $watcher.sliderValue, in: -1000...1000, step: 50, direction: .center) {
VStack {
Text("Compensation")
Text(String(format: "%.0fK", watcher.sliderValue))
}
}
.onChange(of: watcher.sliderValue) { newValue in
ViewModelWatch.shared.session.transferUserInfo(["compensationValue": newValue])
}
}
}
这就是我的模型的样子:
class ViewModelWatch : NSObject, WCSessionDelegate, ObservableObject {
static let shared = ViewModelWatch()
var session: WCSession = .default
@Published var sliderValue = 0.0
init(session: WCSession = .default){
self.session = session
super.init()
self.session.delegate = self
session.activate()
}
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
self.sliderValue = userInfo["compensationValue"] as? Double ?? 0.0
}
}
步骤和正在发生的事情:
在iOS和WatchOS上启动了应用程序。
从iOS应用中发送了“ redsessvalue” UserInfo,并在
didReceiveUserInfo
中收到了Watch App中的“ iOS应用程序”更新中的slider视图。从Watch App中发送了“ redsestvalue” UserInfo,并以
。didReceiveUserInfo
在iOS应用程序中收到,iOS应用程序中的slider视图很好。
问题:
- 现在,当我重复第二步IE时,请从iOS应用程序发送“ redsessvalue” UserInfo来观看应用程序,
compactslider
不更新。
就像一旦@publed var sliderValue = 0.0
由compactslider
编写,当通过didReceive> didreceiveuserinfo
更新时,它不会更新视图。
I'm building a WatchOS companion app. In a View there's a Slider that reads and writes value. This value is being communicated between iOS and WatchOS app.
struct ReadingView: View {
@EnvironmentObject var watcher: ViewModelWatch
var body: some View {
CompactSlider(value: $watcher.sliderValue, in: -1000...1000, step: 50, direction: .center) {
VStack {
Text("Compensation")
Text(String(format: "%.0fK", watcher.sliderValue))
}
}
.onChange(of: watcher.sliderValue) { newValue in
ViewModelWatch.shared.session.transferUserInfo(["compensationValue": newValue])
}
}
}
This is how my model looks like:
class ViewModelWatch : NSObject, WCSessionDelegate, ObservableObject {
static let shared = ViewModelWatch()
var session: WCSession = .default
@Published var sliderValue = 0.0
init(session: WCSession = .default){
self.session = session
super.init()
self.session.delegate = self
session.activate()
}
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
self.sliderValue = userInfo["compensationValue"] as? Double ?? 0.0
}
}
Steps and what's happening:
Ran the apps on both iOS and WatchOS.
Sent "compensationValue" UserInfo from iOS app and received in
didReceiveUserInfo
in the Watch app, the Slider view in iOS app updates just fine.Sent "compensationValue" UserInfo from Watch app and received in
didReceiveUserInfo
in the iOS app, the Slider view in iOS app updates just fine.
Issue:
- Now when I repeat the 2nd step i.e., send "compensationValue" UserInfo from iOS app to Watch app, the
CompactSlider
doesn't update.
It's like once @Published var sliderValue = 0.0
is written by CompactSlider
, it doesn't update views when updated by didReceiveUserInfo
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
私有
添加到此行中,如果您的代码中有一个不使用同一实例的区域中的区域,它将公开。
您应该仅使用
viewModelWatch.shared
,以便它们可以共享信息。viewModelWatch()
的另一个调用将与viewModelWatch.shared
分开Add
private
to this lineIt will expose if there is an area in your code where you are not using the same instance.
You should only be using
ViewModelWatch.shared
so they can share information.Another call of
ViewModelWatch()
will be separate fromViewModelWatch.shared
尝试在主队列上分配值,例如
或制作视图模型
mainactor
如果部署目标版本允许Try to assign value on main queue, like
or make view model
MainActor
if deployment target version allows