Home > Enterprise >  Is there a way to avoid singleton, yet use same class instance in two different classes at the same
Is there a way to avoid singleton, yet use same class instance in two different classes at the same

Time:01-15

I am building an app, it uses three View Controllers in the same screen at the same time. General structure is something like this:

enter image description here

I want to use dependency injection, I have a main service class. This class must be used in SecondVC(every page of the PageVC, the page view controller) and also in the FirstVC, which has the container to contains PageVC. Can I do it with dependency injection, or only way is to use Singleton?

I already wrote code by dependency injection, but without using service class in FirstVC.

Making my service class a dependency of FirstVC, and passing its instance as dependency to PageVC and SecondVC makes any sense? I mean initializing it inside the FirstVC and pass this instance to other ones?

CodePudding user response:

This is a valid pattern however you need to be careful not to create a retain cycle.

  • Related