已发布的对象未更新

发布于 2025-02-11 16:35:05 字数 1834 浏览 2 评论 0原文

我正在构建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        
    }
}

步骤和正在发生的事情:

  1. 在iOS和WatchOS上启动了应用程序。

  2. 从iOS应用中发送了“ redsessvalue” UserInfo,并在didReceiveUserInfo中收到了Watch App中的“ iOS应用程序”更新中的slider视图。

  3. 从Watch App中发送了“ redsestvalue” UserInfo,并以didReceiveUserInfo在iOS应用程序中收到,iOS应用程序中的slider视图很好。

问题:

  1. 现在,当我重复第二步IE时,请从iOS应用程序发送“ redsessvalue” UserInfo来观看应用程序,compactslider不更新。

就像一旦@publed var sliderValue = 0.0compactslider编写,当通过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:

  1. Ran the apps on both iOS and WatchOS.

  2. Sent "compensationValue" UserInfo from iOS app and received in didReceiveUserInfo in the Watch app, the Slider view in iOS app updates just fine.

  3. Sent "compensationValue" UserInfo from Watch app and received in didReceiveUserInfo in the iOS app, the Slider view in iOS app updates just fine.

Issue:

  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌生 2025-02-18 16:35:05

私有添加到此行中,

private init(session: WCSession = .default){

如果您的代码中有一个不使用同一实例的区域中的区域,它将公开。

您应该仅使用viewModelWatch.shared,以便它们可以共享信息。

viewModelWatch()的另一个调用将与viewModelWatch.shared分开

Add private to this line

private init(session: WCSession = .default){

It 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 from ViewModelWatch.shared

荒人说梦 2025-02-18 16:35:05

尝试在主队列上分配值,例如

func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
    DispatchQueue.main.async {
       self.sliderValue = userInfo["compensationValue"] as? Double ?? 0.0        
    }
}

或制作视图模型mainactor如果部署目标版本允许

@MainActor
class ViewModelWatch

Try to assign value on main queue, like

func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
    DispatchQueue.main.async {
       self.sliderValue = userInfo["compensationValue"] as? Double ?? 0.0        
    }
}

or make view model MainActor if deployment target version allows

@MainActor
class ViewModelWatch
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文