English 中文(简体)
快速倡议 B. 目标和清单项目的重划
原标题:SwiftUI doesn t recognise ObservedObject changes and redraw the list items
The bounty expires in 6 days. Answers to this question are eligible for a +50 reputation bounty. ajw is looking for an answer from a reputable source.

我有一个观测主题,从科索沃母亲运动的观点模式得到更新,并受快速倡议清单的约束。 这里的<代码> 序号是独一无二的。

struct MyView: View {
        
      private let viewModel: MyViewModel = MyViewModel()

      @ObservedObject private var state: ObservableViewModel<MyViewModel.State>

      init() {
           state = viewModel.observableState()
           viewModel.onStateChange(callback: { state in
               self.state.value = state
           })
      }
        
      var body: some View {
          NavigationStack {
              List(state.value.items, id: .serial) { device in
                 Item(device: device)
              }
          }
      }
}

extension MyViewModel {
    func observableState() -> ObservableViewModel<MyViewModel.State> {
        return (stateFlow.value_ as! MyViewModel.State).wrapAsObservable()
    }
}
    
class ObservableViewModel<T>: ObservableObject {
      @Published var value: T
        
      init(value: T) {
           self.value = value
      }
}

我观察了我的看法模式的最新情况,没有问题,但清单项目没有把最新物品重新划归SOS。

这些是KMM观察模型数据类别,我使用StateFlow,以获取最新信息。

    data class State(
        val items: List<Item> = arrayListOf(),
        val name: String = ""
    ) : ViewState
    
    data class Item(
        val name: String = "",
        val serial: String = ""
    )

    val state = _state.asStateFlow()

    val stateFlow = state.stateIn(GlobalScope, SharingStarted.Eagerly, initialState)

    fun onStateChange(callback: (State) -> Unit) {
        CoroutineScope(Dispatchers.Main).launch {
            stateFlow.collect {
                callback(it)
            }
        }
    }

It seems SwiftUI doesn t identify the changes to the objects and redraw relevant views. What is the best way to handle this?

不容置疑的是,它总是发生,而只是在我经常得到更新的时候。 在其他时候,它与往常一样,毫无问题地运作和更新了《国际调查》。 添加或删除新物品的,也处以罚款。

问题回答

暂无回答




相关问题
SwiftUI: How do I position a button x points above a sheet?

I am trying to recreate a Maps-style UI with a persistent bottom sheet and controls fixed above. Apple Maps UI with controls above ☝️ Example here, the "Look Around" binoculars button and ...

How to access own window within SwiftUI view?

The goal is to have easy access to hosting window at any level of SwiftUI view hierarchy. The purpose might be different - close the window, resign first responder, replace root view or ...

热门标签