SwiftUI - ObservableObject 创建了多次 [英] SwiftUI - ObservableObject created multiple times

查看:34
本文介绍了SwiftUI - ObservableObject 创建了多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图中创建了一个 ObservableObject.

I have created an ObservableObject in a View.

@ObservedObject var selectionModel = FilterSelectionModel()

我在 FilterSelectionModelinit 函数中放置了一个断点,它被多次调用.因为此视图是 NavigationLink 的一部分,所以我知道它是在那时创建的,并且与它一起创建的是 selectionModel.当我导航到视图时,将再次创建 selectionModel.

I put a breakpoint inside the FilterSelectionModel's init function and it is called multiple times. Because this View is part of a NavigationLink, I understand that it gets created then and along with it, the selectionModel. When I navigate to the View, the selectionModel is created again.

在同一个视图中,我有一个子视图",我在其中将 selectionModel 作为 EnvironmentObject 传递,以便子视图可以更改它.

In this same View I have a "sub View" where I pass the selectionModel as an EnvironmentObject so the sub-view can change it.

AddFilterScreen().environmentObject(self.selectionModel)

当子视图被关闭时,selectionModel 会再次创建,并且对它所做的更改也消失了.

When the sub view is dismissed, the selectionModel is once more created and the changes made to it have disappeared.

有趣的注释:最顶层是 NavigationView.如果我添加

Interesting Note: At the very top level is a NavigationView. IF I add

.navigationViewStyle(StackNavigationViewStyle())

到这个 NavigationView,我的 selectionModel 的变化消失了.但是,如果我添加navigationStyle,则子视图中的 selectionModel 更改仍然存在!(但我不想要一个拆分的导航视图,我想要一个堆叠的导航视图)

to this NavigationView, my selectionModel's changes disappear. BUT if I do not add the navigationStyle, the selectionModel's changes made in the sub view remain!! (But I don't want a split nav view, I want a stacked nav view)

在这两种情况下 - 有或没有 navigationStyle, selectionModel 都会被创建多次.我无法理解这些应该如何可靠地工作.

In both cases - with or without the navigationStyle, the selectionModel is created multiple times. I can't wrap my head around how any of this is supposed to work reliably.

推荐答案

最新的 SwiftUI 更新为这个问题带来了解决方案.(iOS 14 以上)

Latest SwiftUI updates have brought solution to this problem. (iOS 14 onwards)

@StateObject 是我们应该使用的,而不是 @ObservedObject,但仅限于创建该对象的地方,而不是在我们传递相同内容的子视图中的任何地方对象.

@StateObject is what we should use instead of @ObservedObject, but only where that object is created and not everywhere in the sub-views where we are passing the same object.

例如-

class User: ObservableObject {
    var name = "mohit"
}


struct ContentView: View {
  @StateObject var user = User()

  var body: some View {
    VStack {
      Text("name: \(user.name)")
      NameCount(user: self.user)
   }
  }
}


struct NameCount: View {
  @ObservedObject var user

  var body: some View {
    Text("count: \(user.name.count)")
  }
}

在上面的示例中,只有负责创建该对象的视图 (ContentView) 使用 @StateObject 和所有注释来注释 User 对象共享对象的其他视图 (NameCount) 正在使用 @ObservedObject.

In the above example, only the view responsible (ContentView) for creating that object is annotating the User object with @StateObject and all other views (NameCount) that share the object is using @ObservedObject.

通过这种方法,无论何时重新创建父视图,都不会重新创建 User 对象,并且它会持续存在,而您的子视图只是观察相同的 User 对象不必关心它的重新创建.

By this approach whenever your parent view is re-created, the User object will not be re-created and it will persist while your child views just observing to the same User object doesn't have to care about its re-creation.

希望这会有所帮助.快乐编码...

Hope this helps. happy coding...

这篇关于SwiftUI - ObservableObject 创建了多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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