I have this AppModule
class that contains three functions:
@Singleton
@Provides
static FirebaseFirestore provideFirebaseFirestore() {
return FirebaseFirestore.getInstance();
}
@Singleton
@Provides
static CollectionReference provideUsersCollRef(FirebaseFirestore db) {
return db.collection("users");
}
@Singleton
@Provides
static CollectionReference provideBarsCollRef(FirebaseFirestore db) {
return db.collection("bars");
}
I've read that I can use some kind of naming but I didn't find any way to solve this. Beside that, how can I differentiate in my fragment code which one of those reference I inject? Thanks in advance.
Edit:
@Inject
@Named("bars")
BarsDataSource(CollectionReference barsRef) {
this.barsRef= barsRef;
}
That's correct. There is no way for dagger2 to decide which one to provide, right ?
A possible solution is the usage of the @Named
annotation. You will have to name them also at @Inject. EG.
@Named("users")
@Singleton
@Provides
static CollectionReference provideUsersCollRef(FirebaseFirestore db) {
return db.collection("users");
}
@Named("bars")
@Singleton
@Provides
static CollectionReference provideBarsCollRef(FirebaseFirestore db) {
return db.collection("bars");
}
and
@Named("bars")
@Inject
public CollectionReference bars;
or with constructor injection
@Inject
BarsDataSource(@Named("bars") CollectionReference barsRef) {
this.barsRef= barsRef;
}