Different @Singleton & static @Provides in dagger2

假如想象 提交于 2019-12-02 18:15:26
Jeff Bowman

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:

shekar

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!