0

When you want to add a sub-objectgraph to your global object graph you use:

newObjectGraph = objectgraph.plus(new SubModule("SomeConfig"));

Now you are done with SubModule and you want to let it go (or perhaps you want to replace it with SubModule("AnotherConfig"). How would you do that? In fact do I need to do anything? Or I can simply do:

anotherNewObjectGraph = objectgraph.plus(new SubModule("AnotherConfig"));

PS: this question is based on a presentation about Dagger.1 on Android by Jake Wharton.

Kasra
  • 3,045
  • 3
  • 17
  • 34

1 Answers1

0

Not entirely sure if I got you right, but I'll try to shed some light here. Dagger's plus method creates a new graph and leaves the old one intact. From the docs:

Returns a new object graph that includes all of the objects in this graph...

This means that as long as you keep the instance of your "root" object graph (objectGraph in your example) you'll always be able to create your scoped graphs (or sub-graphs if you prefer the naming).

So in your example both newObjectGraph and anotherNewObjectGraph are completly different graphs that only share what's in objectGraph.

Here's a link to the github file for the ObjectGraph.

As long as you don't keep references to the scoped graphs the GC will take care of cleaning them for you.

Fred
  • 16,367
  • 6
  • 50
  • 65