0

I have an observableObject class that holds some data.

Observable Class

class UserManager:ObservableObject {
    @Published
    var profile:userProfileModel = userProfileModel()
    
    @Published
    var settings:Settings = Settings()
    
    @Published var currentView:String
    @Published var isLoggedIn:Bool

and I create and pass it from SceneDelegate as an environmentObject to view hierarchy.

SceneDelegate

@StateObject var userManager = UserManager()
let contentView = loginRoot().environmentObject(userManager)

The problem is that I have a singleton class that syncs with the server and I need to update the data in the UserManager class.

Singleton Class

public class UserModelAPI {
    @ObservedObject var userManager: UserManager = UserManager()
    public static let shared = UserModelAPI()
    
    func syncServer() {
         userManager.isLoggedIn = true
    }

but it doesn't work at all. I can not publish changes from the singleton class.

Mohammad
  • 1
  • 5

1 Answers1

0

inside your singleton you have a new UserManager(), it is different from the SceneDelegate "@StateObject var userManager ...", that's probably why it does not work as you expect. Try this in your SceneDelegate:

@StateObject var userManager = UserModelAPI.shared.userManager
  • I'm not sure if this is going to work, the `UserManager` class holds the data which the UI depends on, and at some points, I have to monitor for any changes. also, in `UserModelAPI` I have to access that data generated before and make some changes to it. – Mohammad Jun 20 '21 at 05:21