不同的@Singleton&静态@Provides在dagger2中 [英] Different @Singleton & static @Provides in dagger2

查看:126
本文介绍了不同的@Singleton&静态@Provides在dagger2中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否可以知道 @Singleton 与VS 静态之间的区别?

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

@Provides static User currentUser(AuthManager authManager) {
    return authManager.currentUser();
}

@Provides @Singleton User currentUser(AuthManager authManager) {
    return authManager.currentUser();
}


推荐答案

这些是非常不同的属性,并且您可以独立拥有一个。所有这些都是有效的:

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(...) {}

要设置阶段, @Provides User 方法说对于此组件或它的依赖项,每次需要用户时都调用此@Provides方法。通常,该方法每次都会返回一个新实例,而Dagger不会保存或缓存该实例。

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 scope 的一个示例,这是一种说法生命周期策略 policy有关创建新实例的频率的好方法。 @Provides @Singleton用户说:对于此组件或依赖项,只需调用此@Provides方法一次,然后保存结果。 @Singleton 碰巧是一个内置的常见情况,但是您也可以想象创建一个 @UserScope (总是返回与此用户相同的实例),或在Android中为 @FragmentScope @ActivityScope

@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.

对于您的特定情况,您可能不希望 @Singleton ,因为它会指示您的组件保存缓存 AuthManager中的值。如果用户值可能会在您的应用程序的整个生命周期内发生变化,则组件将不会反映出来。 (在这种情况下,您还需要确保注入 Provider< User> ,它会更新,而不是 User 不会。)

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.)

暂时保留范围,静态的行为与您期望的完全一样在Java中:如果方法不需要任何实例状态,则可以使其变为 static ,并且虚拟机可以在不准备任何实例状态的情况下调用它。在生成的Component实现中,Dagger将自动静态调用 static 方法,并在传递给Component的Module实例上使用实例方法;在Android中,这可显着提高性能。因为您没有在 currentUser 方法中使用任何实例状态,所以可以很容易地将其设置为 static

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.

进一步阅读:

  • SO: Scopes in Dagger 2
  • Dagger docs: Component (see heading "Scope")

这篇关于不同的@Singleton&amp;静态@Provides在dagger2中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆