mercredi 28 février 2018

How to avoid a Singleton with multiple VCs depending on UserModel

this question is not so technical rather theoretical. And yeah, avoiding singletons can be found all over the internet but let me explain my situation.

I am developing an app where a user can log in and a tab bar with multiple ViewControllers is presented to him. The problem is that almost all of the ViewControllers need an access to the UserModel (class) that is logged in. I don't want every ViewController to load the user from a storage, so the first solution I came up with was a Singleton. But you know, singletons shouldn't be used to avoid passing variables around or just as a global variable. Searching the web I found that dependency injection might be the solution however it would mean that the TabBarVC would be initialized with UserModel to be able to initialize all its ViewControlllers with UserModel and it would be a lot of passing variables around.

This situation sounds like a typical example of avoiding singletons but unfortunately, I wasn't able to found an ideal solution on the internet.

Any ideas how to solve this problem? What might be the best / optimal solution for having so many ViewControllers depend on the UserModel? How to avoid using a singleton in this case?

Thank you for any ideas!

Aucun commentaire:

Enregistrer un commentaire