I was having some problem with Android Architecture Component. What I am trying to do is from the view model, I will execute the functions inside repository class. Here is my view model:
private final ReservationRepository reservationRepository;
private final UserRepository userRepository;
private LiveData<List<ReservationEntity>> reservations;
@Inject
public ReservationViewModel(ReservationRepository reservationRepository, UserRepository userRepository) {
this.reservationRepository = reservationRepository;
this.userRepository = userRepository;
reservations = reservationRepository.loadReservations();
}
public LiveData<List<ReservationEntity>> getAllReservations() {
return reservations;
}
Then in my repository class:
private final ReservationDao reservationDao;
@Inject
public ReservationRepository(ReservationDao reservationDao) {
this.reservationDao = reservationDao;
}
public LiveData<List<ReservationEntity>> loadReservations() {
return reservationDao.getAllReservation();
}
In my DAO class:
@Query("SELECT * FROM reservation")
LiveData<List<ReservationEntity>> getAllReservation();
I got one AppModule to @Provide the @Inject:
@Provides
@Singleton
ReservationDatabase provideReservationDatabase(Application application) {
return Room.databaseBuilder(application,ReservationDatabase.class,
ReservationDatabase.DATABASE_NAME).allowMainThreadQueries().build();
}
@Provides
@Singleton
ReservationDao provideReservationDao(ReservationDatabase reservationDatabase) {
return reservationDatabase.reservationDao();
}
I managed to build the project successfully. However, after successfully built gradle, it does not install the apps onto the emulator. Any ideas?
Thanks!
In my case, a Gradle Sync solved the issue.
You can do the following to resolve your problem
You can turn off the instant Run.
File>Settings>Build , Execution , Deployment>Instant Run>Uncheck the Instant Run.
After that try to run your app on emulator again.
Restart Android studio
File->Invalidate Caches / Restart
Perform Clean Build
Build->Clean Project
Try to Sync your project
File-> Sync project
Make sure Project location does not contain the special character.
E:\Android_Projects\T&PUIET,KUK\app\build\outputs\apk\app-debug.apk
close android studio > rename folder containing the special character(here T&PUIET,KUK ) > restart android studio.
Hope this Solution helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With