简体   繁体   English

SwiftUI 可以将 ObservableObject 放入另一个 ObservableObject 中吗?

[英]SwiftUI Is it ok to put an ObservableObject inside another ObservableObject?

I have a view called PurchaseView .我有一个名为PurchaseView的视图。 This view displays details about the purchase, what was purchased and who purchased it.此视图显示有关购买、购买的内容和购买者的详细信息。 What I'm doing is that in this view im putting both the ItemView and ClientView inside PurchaseView .我正在做的是,在这个视图中,我将ItemViewClientView都放在PurchaseView中。 ItemView and ClientView are shared and there are used in other parts of my app. ItemViewClientView是共享的,并且在我的应用程序的其他部分中使用。 They have their own ViewModels.他们有自己的 ViewModel。

I have also tried to put ItemViewModel and ClientViewModel inside PurchaseViewModel but I do not know if it is ok to put an ObservableObject inside another ObservableObject .我也尝试将ItemViewModelClientViewModel放入PurchaseViewModel但我不知道是否可以将ObservableObject放入另一个ObservableObject Is this a good approach or there should not be any ObservableObject inside an ObservableObject ?这是一个好方法还是ObservableObject内不应该有任何ObservableObject Which one of the following is better?以下哪一项更好?

This?这个?

class PurchaseViewModel: ObservableObject {
    let clientViewModel: ClientViewModel
    let itemsViewModel: ItemViewModel

    //
}

Or this?或这个?

struct PurchaseView: View {
    @ObservedObject var purchaseViewModel: PurchaseViewModel
    @ObservedObject var itemViewModel: ItemViewModel
    @ObservedObject var clientViewModel: ClientViewModel

    var body: some View {
        //
    }
}

Purchase model:购买 model:

class Purchase {
    let id: String
    let total: Double
    // ...
    var item: Item?
    var client: Client?
}

Your first solution will not work as the changes to the nested ObservableObjects aren't propagated upwards:您的第一个解决方案将不起作用,因为对嵌套 ObservableObjects 的更改不会向上传播:

class PurchaseViewModel: ObservableObject {
    let clientViewModel: ClientViewModel
    let itemsViewModel: ItemViewModel
    ...
}

A workaround can be found here: How to tell SwiftUI views to bind to nested ObservableObjects .可以在此处找到解决方法:如何告诉 SwiftUI 视图绑定到嵌套的 ObservableObjects


Your second approach is right and will work for most cases:您的第二种方法是正确的,并且适用于大多数情况:

struct PurchaseView: View {
    @ObservedObject var purchaseViewModel: PurchaseViewModel
    @ObservedObject var itemViewModel: ItemViewModel
    @ObservedObject var clientViewModel: ClientViewModel
    ...
}

If you share an ObservableObject for many views you can inject it to the environment instead and access as an @EnvironmentObject .如果您为多个视图共享一个ObservableObject ,则可以将其注入环境并作为@EnvironmentObject访问。


Alternatively you can make your nested ObservableObjects to be structs :或者,您可以将嵌套的 ObservableObjects 设置为structs

class PurchaseViewModel: ObservableObject {
    @Published var clientViewModel: ClientViewModel // <- add `@Published`
    ...
}
struct ClientViewModel { // <- use `struct` instead of `class`
    ...
}

Note that your ClientViewModel will become a new struct every time it (or any of its properties) changes - this solution shouldn't be overused (especially for complex ViewModels).请注意,您的ClientViewModel将在每次它(或其任何属性)更改时成为一个新结构 - 此解决方案不应过度使用(尤其是对于复杂的 ViewModel)。

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

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