0

I have the following class:

final class NetworkController: ObservableObject {
    
    init() {
        if #available(iOS 15.0, *) {
            print("⚠️ Inside init")
            Task(priority: .medium){
                await configureAmplify()
                await checkUserSignedIn()
            }
        } else {
            print(" Failed to Initialize Amplify")
        }
    }
    
    
    @Published var authState: AuthState = .guess    // Observes state changes in the session

...

}

If I try to initialize it to access some function inside, like this:

final class DataStorePersistanceHandler: ObservableObject {

var networkController = NetworkController()

...

}

I get the NetworkController class looping multiple times, my guess is that it's recreating the view and therefore calling it multiple times, so my question is, how can I initialize it without doing so? I just want to have it initialized for when I need it.

Thanks

Albert
  • 99
  • 7
  • Is this a school thing? I'm having Deja Vu here. Every time you call `NetworkController()` you are creating a different instance. One does not know what the other is doing. Make `NetworkController` a singleton or share the original instance some how. – lorem ipsum Aug 05 '21 at 21:50
  • lool no no a school thing. `NetworkController`'s instance is being shared in the environment with `@StateObject` and `.environmentObject` is that what you mean? – Albert Aug 05 '21 at 22:12
  • see if this answer your question regarding nesting ObservableObjects. https://stackoverflow.com/questions/58437861/how-to-tell-swiftui-views-to-bind-to-more-than-one-nested-observableobject – workingdog support Ukraine Aug 05 '21 at 22:26
  • @workingdog no it doesn't, so the question is, how do I call in a class an existing `ObservableObject` class just to reference it – Albert Aug 05 '21 at 22:41
  • can you show where and how you are calling these or at least DataStorePersistanceHandler. – workingdog support Ukraine Aug 05 '21 at 22:48
  • "NetworkController's instance is being shared in the environment with @StateObject and .environmentObject is that what you mean?" You have not posted code that shows this, and the only code you have posted, your `DataStorePersistanceHandler{}` specifically does NOT use a singleton, but initializes a new instance. – Yrb Aug 06 '21 at 14:33

0 Answers0