Sunday, May 31, 2026
HomeiOS Developmentios - Utilizing an ObservableObject singleton class for present session

ios – Utilizing an ObservableObject singleton class for present session

[ad_1]

I am creating an IOS utility on SwiftUI the place a person can be a part of teams and work together with different customers, and I am curious what the results of using an ObservableObject singleton class for the present person’s session are, which might instantly change a typical EnvironmentObject utilized in the identical means. What I am making an attempt to do is remove instantly injecting the EnvObj view mannequin into subsequent view fashions.

So as a substitute of this (which happens in lots of locations all through the app):

class ChildViewModel: ObservableObject { 
    let session: SessionViewModel

    init(session: SessionViewModel, ...) { 
        self.session = session
        ...
    }

    ...
}

It could change into:

class ChildViewModel: ObservableObject {
    let session = Session.shared

    init(...) { ... }

}

the place Session is one thing like:

class Session: ObservableObject { 
    @Revealed var person: Consumer?
    @Revealed var usersGroups: [GroupModel] = []

    // As singleton
    static let shared = Session()
    personal init() { }

    // As environmentObject - Initialized after person is authenticated
    init() { 
        ...
        fetchUsersGroups()
    }

    func fetchUsersGroups() async { 

    }
}

If I do know that the person will probably be outlined by this level when the ChildViewModel is initialized, I determine I can create this singleton class which I can pull anyplace inside the app after the person is logged in.

What points would this implementation create?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments