UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Monitoring Changes in an Array of @Observable Classes

Forums > SwiftUI

I have an @Observable class called ParentClass with a property that is an array of another @Observable class called ChildClass. The ChildClass has an isOn boolean property. When the isOn property of a ChildClass is changed to false, I would like the ParentClass to be internally aware of the change and remove that ChildClass instance from the array in the ParentClass.

import SwiftUI
import Observation

@Observable class ParentClass {
    var children: [ChildClass]
    //Some kind of monitoring or didSet or something?

    init(children: [ChildClass]) {
        self.children = children
    }
}

@Observable class ChildClass: Identifiable {
    let id = UUID()
    var isOn: Bool = true
}

struct MainView: View {
    @State var parentClass: ParentClass

    init() {
        _parentClass = State(wrappedValue: ParentClass(children: [.init(), .init(), .init()]))
    }

    var body: some View {
        Text("Some body")
    }
}

I know I can attach an onChange modifier to the views that have the ParentClass in them, but there are a lot of them, and I would have to add the same onChange code to every place where the isOn property could change, which would be tedious and somewhat fragile. So is there a way I can monitor entirely within the ParentClass itself for when the isOn property of one of the ChildClass instances changes so that it can be removed? Something like a did/willSet or withObservationTracking or something like that?

   

@Obelix This is effectively the solution I have in place now, such that I need to make a function call from within a view to the ParentClass/House class when a ChildClass/Wizard property changes. This does work, but it means that I need to pass around the ParentClass to every subview where a ChildClass could change, even if the rest of the ParentClass isn't needed there. And then I need to call an additional ParentClass.removeOldChildren/House.expelWizards function to every place the bool can change. In my app the bool within ChildClass can change in a lot of different places in different views, so that's a lot of additional class passing. In my case I'd also like to avoid passing ParentClasses around through the environment since I have a lot of different ParentClasses at any one time and they are also used in a bunch of different places at the same time.

So I was hoping I could monitor for the ChildClass bool change fully within the ParentClass itself using some kind of trigger inside it, instead of having to pass a lot of them around and call its removeOldChildren function from every view where a ChildClass might change.

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.