简体   繁体   English

如何在 SwiftUI 上的可观察对象中使用可观察对象 class?

[英]How can I use an observableobject class in an observableobject on SwiftUI?

I attached an image.我附上了一张图片。 Please see it.请看。

1个

As far as I know the "View" is only view.据我所知,“视图”只是视图。 It's not controller. So Im developing like Way 1. But I faced a problem that how can I use observableobject in another observableobject?它不是 controller。所以我像方法 1 一样开发。但是我遇到了一个问题,我如何在另一个 observableobject 中使用 observableobject?

I thought if I pass a parameter with the observableobject the problem will be clean.我想如果我用 observableobject 传递一个参数,问题就会迎刃而解。 But I think it is bad way..但我认为这是不好的方式..

So I thought way 2. But the way is the "View" is not only view.所以我想到了方法2。但是方法是“视图”不仅仅是视图。 It is view and controller.它是视图和 controller。

So Im confused the way2 is bad way or not.所以我很困惑 way2 是不是坏方法。

Which way is good way?哪种方式好? and Im wondering other SwiftUI developers how to develop about this case.我想知道其他 SwiftUI 开发人员如何开发这个案例。

Please advice me if you think there is better way than way1 & way2.如果您认为有比 way1 和 way2 更好的方法,请告诉我。

Summary概括

Q1. Q1。 Way1 - How can I use observableobject in another observableobject? Way1 - 如何在另一个可观察对象中使用可观察对象? (singltone? like static shared) (单音?喜欢 static 共享)

Q2. Q2。 Way2 - Is it correct way? Way2 - 这是正确的方法吗? (View = view + controller) (视图=视图+控制器)

Q3. Q3. Your advice.你的建议。

Env环境

Xcode 14.2 Xcode 14.2

Swift 5.7.2 Swift 5.7.2

Here is the sample code for your question:这是您的问题的示例代码:

struct MainView: View {
     @StateObject var mainVM = MainViewModel()
     @ObservedObject var discoveryVM:DiscoveryViewModel

     var body: some View {
           ZStack {
                ScrollView {
                     // if data changed in DiscoveryViewModel view will automatically upadte
                     ForEach(discoveryVM.showProfiles, id: \.self) { profile in
                          MyView(profile: Profile)
                     }
                }
           }
           .onReceive(discoveryVM.$toggle) { status in 
           // change something in MainViewModel when toggle in DiscoveryViewModel
                mainVM.toggle = status // change variable(rearly used)
                mainVM.doSomething() // call fucntions
           }
     }
}


class MainViewModel: ObservableObject {
     @Published var toggle: Bool = false
     
     func doSomething() {
        print("Do something")
     }
}



class DiscoveryViewModel: ObservableObject {
     @Published var data: [ProfileModel] = []
     @Published var toggle: Bool = false
}

ObservableObjects are mostly used where there is nesting of views ObservableObjects主要用于有嵌套视图的地方

SwiftUI is a new architecture, old design patterns don't really fit. SwiftUI 是一种新架构,旧的设计模式并不适合。 In SwiftUI the View struct isn't the view in the MVC sense, that view layer (eg UIView/NSView objects) is generated automatically for us based diffing the View structs which are super fast efficient value types.在 SwiftUI 中, View结构不是 MVC 意义上的视图,视图层(例如 UIView/NSView 对象)是根据超快速高效值类型的View结构的差异自动为我们生成的。 View state is held in property wrappers like @State which essentially makes the value behave like an object, eg it has a "memory".视图 state 保存在像@State这样的属性包装器中,这实际上使该值的行为类似于 object,例如它具有“内存”。 You can pass this down to sub-Views as let for read access or @Binding var for write access, SwiftUI will track dependencies and call body on the affected View structs when necessary.您可以将其传递给子视图,如let用于读取访问或@Binding var用于写入访问,SwiftUI 将在必要时跟踪受影响的View结构上的依赖项和调用body

Normally in SwiftUI we only have a single ObservableObject that holds the model structs in @Published properties.通常在 SwiftUI 中,我们只有一个ObservableObject ,它在@Published属性中保存 model 结构。 Usually there are 2 singletons, one shared that loads from disk and another preview pre-loaded with test data for driving SwiftUI previews.通常有 2 个单例,一个shared从磁盘加载,另一个preview预加载测试数据以驱动 SwiftUI 预览。 This object is shared across View structs using .environmentObject这个 object 使用.environmentObject在视图结构之间共享

If you attempt to use multiple ObservableObject for view data instead of @State you'll run into major problems, actually the kind of consistency problems that SwiftUI's use of value types was designed to eliminate.如果您尝试对视图数据使用多个ObservableObject而不是@State ,您将遇到主要问题,实际上是 SwiftUI 使用值类型旨在消除的那种一致性问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM