发布者内部的发布者不会触发SwiftUi重新渲染

编程入门 行业动态 更新时间:2024-10-24 19:21:27
本文介绍了发布者内部的发布者不会触发SwiftUi重新渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在EnvironmentObject中为我的应用创建了一个状态"对象像这样:

I made a "State" Object for my App in EnvironmentObject Like this:

class AppState: ObservableObject { @Published var counter = Counter() }

要将其添加到我的应用程序中,我使用:

To add this to my App I use:

window.rootViewController = UIHostingController(rootView: contentView.environmentObject(state))

计数器是后台任务

class Counter: ObservableObject { @Published var ammount: Double var timer = Timer() init() { self.ammount = 0.0 self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(setCoords), userInfo: nil, repeats: true) } @objc private func setCoords() { DispatchQueue.main.async() { () -> Void in self.ammount = self.ammount + 0.1 print(self.ammount) } } }

在我看来,我有:

struct ContentView: View { @EnvironmentObject var state: AppState @State var isVisible = true var body: some View { VStack { Button(action: { self.isVisible.toggle() }) { Text("Button") } if isVisible { Text(state.counter.ammount.description) } } } }

因此,基本上,我希望每秒能看到UI更新中的计数器.但是UI不会更新.我看到打印语句每秒触发一次,如果我从UI按钮 self.isVisible.toggle()触发UI更新,则计数器也会更新.

So basically, what I was hoping was that I see the counter in my UI update after every second. But the UI does not update. I see the print statement fire every second and if I trigger the UI update from the UI Button self.isVisible.toggle() then the counter will also update.

如何在不将计数器移入View或在状态对象中实现计数器的情况下解决此问题?

How could I solve this problem without moving the counter into the View or implementing it in the state object?

推荐答案

您的假设不正确.您将 Counter 设置为 ObservableObject ,但从未观察到.

Your assumption is not correct. You made Counter as ObservableObject, but never observe it.

将 state 设置为 @EnvironmentObject ,您会观察到属性 var counter 的变化,但内部没有变化.它是类,引用类型,而不是结构,因此实例内部属性的修改不会导致实例本身的修改-引用保持不变,因此 state 不变.

Making state as @EnvironmentObject you observe changes of property var counter, but not inside it. It is class, reference type, not struct, so modifications of internal properties of instance does not result in modification of the instance itself - the reference remains the same, so state is not changed.

这是使快照正常工作的一种方法-通过引入显式的发布者事件来观察并更新依赖的本地视图状态:

Here is one of the approach to make your snapshot working - by introducing explicit publisher events observing and update dependent local view state:

@State var currentCounter: String = "" var body: some View { VStack { Button(action: { self.isVisible.toggle() }) { Text("Button") } if isVisible { Text(currentCounter) .onReceive(state.counter.$ammount, perform: { value in self.currentCounter = value.description }) } } }

更多推荐

发布者内部的发布者不会触发SwiftUi重新渲染

本文发布于:2023-11-13 14:22:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584559.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发布者   SwiftUi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!