-
Notifications
You must be signed in to change notification settings - Fork 3
Conversation
@@ -83,4 +90,12 @@ EventDao provideEventDao(GpgDatabase db) { | |||
AttractionFlagDao provideAttractionFlagDao(GpgDatabase db) { | |||
return db.attractionFlagDao(); | |||
} | |||
|
|||
@Singleton |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So what's going on here is that we use Dagger for dependency injection (sidenote: a dependency injection library called Dagger strikes me as...kind of grim). We need to create just a single instance of both ExecutorService
and Handler
at the top level of the app so that they're available to any parts of the app that need them, but the place where we need them, DestinationRepository
, is nested many, many levels down from the top. If we did this by hand, we'd have to pass an instance of ExecutorService
down through every nested constructor call in the application to get it down to the level where it's actually used.
What Dagger does is the @Singleton
decorator tells it to know about this resource and to make sure that only one of it ever exists, and the @Provides
decorator tells it that if some other part of the app asks for one of these things, to get it from here.
Then, in places where we need one or more parameters that Dagger knows about, we can add the @Inject
decorator and Dagger will inspect the parameters to the constructor, compare them to the different resources that it knows about, and, assuming it finds a match, it'll auto-generate all the code to pass that resource all the way down to where it gets used so that we don't write it by hand.
You can see this at work in DestinationRepository.java. I don't 100% understand what heuristics it uses to figure out which resource to attach to which constructor parameters (does it use the type signature alone or does it also parse the name somehow?) but it seems to be working, so that's good enough for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really helpful context, thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -83,4 +90,12 @@ EventDao provideEventDao(GpgDatabase db) { | |||
AttractionFlagDao provideAttractionFlagDao(GpgDatabase db) { | |||
return db.attractionFlagDao(); | |||
} | |||
|
|||
@Singleton |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really helpful context, thank you!
Thanks for reviewing, @rachelekm ! I'm going to go ahead and merge now. |
Overview
AsyncTask
has been deprecated, so this stops using it.Demo
Here we are running on a Nexus 4 (2012) on Android SDK 22
And on a Pixel 5 (2020) running Android SDK 33
Notes
Testing Instructions
Closes #213