I'm developing an IOS application on SwiftUI where a user can join groups and interact with other users, and I'm curious what the consequences of utilizing an ObservableObject singleton class for the current user's session are, which would directly replace a typical EnvironmentObject used in the same way. What I'm trying to do is eliminate directly injecting the EnvObj view model into subsequent view models.
So instead of this (which occurs in many places throughout the app):
class ChildViewModel: ObservableObject {
let session: SessionViewModel
init(session: SessionViewModel, ...) {
self.session = session
...
}
...
}
It would become:
class ChildViewModel: ObservableObject {
let session = Session.shared
init(...) { ... }
}
where Session is something like:
class Session: ObservableObject {
@Published var user: User?
@Published var usersGroups: [GroupModel] = []
// As singleton
static let shared = Session()
private init() { }
// As environmentObject - Initialized after user is authenticated
init() {
...
fetchUsersGroups()
}
func fetchUsersGroups() async {
}
}
If I know that the user will be defined by this point when the ChildViewModel is initialized, I figure I can create this singleton class which I can pull anywhere within the app after the user is logged in.
What issues would this implementation create?