Different @Singleton & static @Provides in dagger2

前端 未结 2 1016
小蘑菇
小蘑菇 2021-01-31 10:35

May I know the different between @Singleton VS static Provides in dagger2?

@Provides static User currentUser(AuthManager authMa         


        
相关标签:
2条回答
  • 2021-01-31 10:58

    These are very different attributes, and you can have one or the other independently. All of these are valid:

    @Provides User currentUser(...) {}
    @Provides static User currentUser(...) {}
    @Provides @Singleton User currentUser(...) {}
    @Provides @Singleton static User currentUser(...) {}
    

    To set the stage, a @Provides User method says "for this Component or its dependencies, call this @Provides method every time you need a User". Typically the method will return a new instance every time, and Dagger won't save or cache the instance.

    @Singleton is an example of a scope, which is a fancy way to say lifecycle policy or policy for how often to create a new instance. @Provides @Singleton User says "for this Component or dependencies, just call this @Provides method once, and save the result". @Singleton happens to be a built-in common case, but you could also imagine creating a @UserScope (always return the same instance for this User), or in Android a @FragmentScope or @ActivityScope.

    For your specific case, you probably don't want @Singleton, because it would instruct your component to save or cache the value from AuthManager. If the User value may change across your application's lifetime, the Component wouldn't reflect that. (In that case you would also want to make sure to inject Provider<User>, which would update, rather than User which would not.)

    Leaving scopes behind for a moment, static behaves exactly the way you would expect it to in Java: If a method doesn't require any instance state, you can make it static, and your virtual machine can call it without preparing any instance state. In your generated Component implementation, Dagger will automatically call static methods statically, and instance methods on the Module instance you pass into your Component; in Android this results in a sizable performance increase. Because you don't use any instance state in your currentUser method, it can easily be made static.

    Further reading:

    • SO: Scopes in Dagger 2
    • Dagger docs: Component (see heading "Scope")
    0 讨论(0)
  • 2021-01-31 11:16

    With @Singleton annotation only one instance of User object will be created throughout the application lifecycle.

    static on @Provides methods introduced recently to make the invocation of method faster by 15 to 20% as mentioned here. There will be multiple instances of User object if we call this method multiple times.

    0 讨论(0)
提交回复
热议问题