发布者内部的发布者不会触发SwiftUi重新渲染 [英] Publisher inside a Publisher does not trigger SwiftUi re-render

查看:74
本文介绍了发布者内部的发布者不会触发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重新渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆