Home / IOS Development / singleton, protocols … keeps track of user status. : iOSProgramming

singleton, protocols … keeps track of user status. : iOSProgramming

Hi reddit,
I am working on a personal / college project, and I have read a number of articles on how I can track the condition of some data in the app, and I am completely confused.

I have a tab with a few display controls. Once the user is logged in, I want it to change display across display controllers, or at least know about the update when I switch to them.

What I have right now is a singleton UserManager. It asynchronization retrieves user data and saves them as User. Other display controllers access the data through the user variable. A problem occurs that viewControllers does not call fetchData and when they gain access User it can be in loading state. What would be a solution to that issue?

I also tried Combine (I work with UIKit) where I had singleton as ObservedObject, user as Published and in separate view controller I had

    private var cancellable: AnyCancellable?

    override func viewDidLoad() {

        cancellable = UserManager.shared.objectWillChange.sink { [weak self] in

in this solution the problem was that the user sent data prematurely or inconsistently. It often sent signals just before the value was changed and not after. Because of that, it gave an outdated value.

Can someone give me a few insights into what I can do wrong, thank you!
You can also point me to an article.

Source link