在匕首是子图缓存内的单身或者当一个新的活动子图的构造将他们总是会重现? [英] In Dagger are Singletons within the sub-graph cached or will they always be recreated when a new activity sub-graph is constructed?

查看:90
本文介绍了在匕首是子图缓存内的单身或者当一个新的活动子图的构造将他们总是会重现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用匕首创建活动的具体对象图。在这个子图,我利用一个Singleton的我的presentationModel

I'm using Dagger to create activity specific object graphs. Within this subgraph, I make use of a Singleton MyPresentationModel.

当我离开我的活动,并再次输入活动,我的期望是,在创建活动的特定对象图的新实例,这反过来会创造辛格尔顿的新实例我的presentationModel 凭借@Singleton每匕首语义。看到这个如此回答细节),这将随后持续活动的特定对象图的寿命。

When i exit my activity, and enter the activity again, my expectation is that a new instance of the activity specific object graph is created, which in turn would create a new instance of Singleton MyPresentationModel (by virtue of the @Singleton semantic per Dagger. See this So answer for specifics) which would then last for the life of the activity specific object graph.

然而,这不是我观察,每次创建活动的特定对象图的时候,使用我的presentationModel的同一个实例。我加了点调试进入我的presentationModel 的构造。在第一次进入我们的构造。随后甚至在活动退出和再入,我们不进入的构造函数(因为这一点的 UserSession 正在我的presentation模型中使用使用来自旧值第一个构造器注入)。

However, this is not what i'm observing, every time the activity specific object graph is created, the same instance of MyPresentationModel is used. I added a debug point into the constructor of MyPresentationModel. The very first time we enter the constructor. Subsequently even on activity exits and reentries, we don't enter the constructor (and because of this the UserSession being used within my Presentation model uses the old value from the very first constructor injection).

虽然我可以在技术上与外部公众的setter解决由我presentaitonModel内重新设置UserSession这个问题,我想更好地了解活动的特定对象图的创建/销毁的机制。

While i can technically solve the problem by re-setting UserSession inside MyPresentaitonModel with an external public setter, I want to understand better the mechanics of the activity specific object graph creation/destruction.

通过在我的onDestroy作废图,是否还意味着有在以后被重用我的子图内的单身的可能性? (可能直到他们是真正的GCed?)

By nullifying the graph in my onDestroy, does that still mean that there is a possibility of the Singletons within my subgraph being reused at a later point ? (possibly until they are truly GCed?)

下面是一些code:

 // MyAppModule
 @Module(
    includes = { UserSession.class},
    injects = { MyApplication.class })
public class MyAppModule {

  private final MyApplication _app;

  MyAppModule(MyApplication app) {
    _app = app;
  }
  // ...
}


  // Main Activity

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    _activityObjectGraph = MyApplication.get()
        .getObjectGraph()
        .plus(Arrays.<Object>asList(new SubModule()).toArray());

    // Inject ourselves so subclasses will have dependencies fulfilled when this method returns.
    _activityObjectGraph.inject(this);
  }

  @Override
  protected void onDestroy() {
    _activityObjectGraph = null;
    // this eagerly allows GC, but doesn't necessarily destroy the subgraph ?
    super.onDestroy();
  }

 // SubModule
  @Module(injects = { MyPresentationModel.class, MainActivity.class },
          addsTo = MyAppModule.class,
          library = true)
  public class SubModule {}

}

// MyPresentationModel
@Singleton
public class MyPresentationModel {

  private UserSession _session;

  @Inject
  public MyPresentationModel(UserSession session) {
    _session = session;
  }

  public void someMethodThatUsesSessionInfo() {
      // _session.getUser() ...
  }
}

@weefbellington发布了非常丰富的答案,但阅读它使我意识到我的问题不是具体,不够清晰。这里是尝试2:

@weefbellington posted a very informative answer, but reading it made me realize my question was not specific and clear enough. Here's attempt 2:

MyAppModule (主图) - >提供一个Singleton UserSession

MyAppModule (main graph) -> provides a Singleton UserSession

MySubModule (子图plused到MyAppModule) - >提供了活动的具体辛格尔顿我的presentationModel 这需要一个 UserSession (提供我的MyAppModule)建设。

MySubModule (sub graph plused onto MyAppModule) -> provides "activity specific" Singleton MyPresentationModel which requires a UserSession (provided my MyAppModule) on construction.

我现在关闭活动,破坏MySubModule(同时也希望我的presentationModel 这是一个Singleton),我更新了一些新的信息UserSession。

I now close the activity, destroying MySubModule (and also hopefully MyPresentationModel which is a Singleton), I update UserSession with some new information.

我再次打开MainActivity,从而重新创建从 MySubModule 子图,这inturn提供了一个我的presentationModel

I open MainActivity again, thus re-creating the sub-graph from MySubModule, which inturn provides a MyPresentationModel.

我注意到的问题是,我的presentationModel这是当地辛格尔顿不被再次重建即code的这一部分:

The issue I'm noticing is that MyPresentationModel which is the local Singleton is not being reconstructed again i.e. this part of the code:

  @Inject
  public MyPresentationModel(UserSession session) {
    _session = session;
  }

时,只会被调用一次。我的期望是,code的这一部分会被再次运行,并在 UserSession 将被从主图又拉又因为它是更新,将举行更新的值。我的问题是:反正该子图缓存内的单身或者当一个新的活动子图被产生将他们总是被重新创建

is only ever being called once. My expectation was that this part of the code would be run again, and the UserSession would be pulled again from the Main graph and since it was updated, it would hold the updated values. My question is: are Singletons within the sub-graph cached in anyway or will they always be recreated when a new activity sub-graph is spawned?

推荐答案

如何我的presentationModule 注入取决于如何指定的模块。例如,假设你是注射类

How MyPresentationModule is injected depends on how your modules are specified. For example, assume that you are injecting the class Foo:

public class Foo {
    private final MyPresentationModel model;
    @Inject
    public Foo(MyPresentationModel model) {
        this.model = model;
    }
}

如果您的模块结构像(A),那么我的presentationModel 单将被注入到由主对象图:

If your modules are structured like (A), then the MyPresentationModel singleton will be injected into Foo by the main object graph:

例A

@Module(injects = { Foo.class })
public class MainModule { ... }

@Module(addsTo = MainModule.class, injects = { MyPresentationModel.class })
public class SubModule { ...}

另外,如果你的模块结构像(B),那么我的presentationModel 单将被注入到的子图:

示例B

@Module
public class MainModule { ... }

@Module(addsTo = MainModule.class, injects = { Foo.class, MyPresentationModel.class })
public class SubModule { ... }

在您的特定情况下,因为你已经指定了 MyAppModule 内喷射所有MyApplication ,我猜你正在尝试注入我的presentationModel 到您的应用程序类。这可能不是你想要做什么。你可能想使用子模块,如(C)注入到你的Activity类此。

In your particular case, since you have specified that MyAppModule injects MyApplication, I would guess that you are trying to inject MyPresentationModel into your Application class. This is probably not what you want to do. You probably want inject this into your Activity class using the submodule, as in (C).

例C

@Module(injects = { MainActivity.class, MyPresentationModel.class },
    addsTo = MyAppModule.class,
    library = true)
public class SubModule { ... }

public class MainActivity {
    @Inject MyPresentationModel presentationModel;
    ...
}

如果你做到这一点,我的presentationModel单将被绑定到活动子而不是主图,当活动被破坏,应处置。

If you do this the MyPresentationModel singleton will be bound to the Activity subgraph instead of the main graph, and should be disposed when the Activity is destroyed.

一旦你有一个匕首手柄,你可能想看看砂浆,它给你更精细在ObjectGraph subscop​​es的创建和销毁-grained控制。

Once you have a handle on Dagger, you might want to check out Mortar, which gives you finer-grained control over creation and destruction of ObjectGraph subscopes.

这篇关于在匕首是子图缓存内的单身或者当一个新的活动子图的构造将他们总是会重现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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