swift - Publisher 内的 Publisher 不会触发 SwiftUi 重新渲染

标签 swift swiftui combine

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

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

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

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

计数器是后台任务

class Counter: ObservableObject {
    
    @Published var amount: Double
    
    var timer = Timer()
    
    init() {
        self.amount = 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.amount = self.amount + 0.1
            print(self.amount)
        }
    }  
}

在我看来我有:

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.amount.description)
            }
        }
    }
}

所以基本上,我希望每秒都能在我的 UI 更新中看到计数器。但是用户界面没有更新。我看到打印语句每秒触发一次,如果我从 UI 按钮 self.isVisible.toggle() 触发 UI 更新,那么计数器也会更新。

如果不将计数器移动到 View 中或在状态对象中实现它,我如何解决这个问题?

最佳答案

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

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

这是使快照工作的方法之一 - 通过引入显式发布者事件来观察和更新相关的本地 View 状态:

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

关于swift - Publisher 内的 Publisher 不会触发 SwiftUi 重新渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59452113/

相关文章:

swift - tableView 内的 collectionView 返回到滚动时的第一个索引

swift - 带有 SwiftUI 的 tvOS 上的 NavigationLink 按钮不起作用

swift - 在 SwiftUI 中向 VStack 动态添加元素

ios - 具有双向绑定(bind)的自定义 SwiftUI View

json - 如何使用组合从 SwiftUI 中的 URL 解析单个 JSON 对象?

ios - 在 SwiftUI 中从 @ObservedObject 获取更新的值

arrays - fatal error : Index out of range (Arrays)

ios - 如何将多个 UIDynamicItem 相互关联

ios - EKEventStore 请求访问不起作用

如果 ObservedObject 属性更改,SwiftUI 导航