Room Empty Db after restarting the application

I am trying to use Room for my new application. But whenever I restart the application, I can not get the old data. It looks like my application creates a new database instance each time, but I'm not sure why. Here are my classes based on the MVP pattern.

Edit: I just checked again, and I see that the auto-generated identifier for the object (MyModel) does not get reset to 1, but when I get the data, I only get the collection inserted in this session.

DAO

@Dao public interface MyDao { @Insert(onConflict = OnConflictStrategy.REPLACE) List<Long> insertModels(List<MyModel> models); @Query("SELECT * FROM MyModel") List<MyModel> getModels(); } 

Database

 @Database(entities = {MyModel.class}, version = 1) public abstract class MyDatabase extends RoomDatabase { public abstract MyDao myDao(); } 

DatabaseModule

 @Module public class DatabaseModule { @Provides @Singleton MyDatabase provideMyDatabase(Application context) { return Room.databaseBuilder(context.getApplicationContext(), MyDatabase.class, "MyDB").build(); } @Provides @Singleton MyDao provideMyDao(MyDatabase myDatabase) { return myDatabase.myDao(); } } 

Data source

 @Singleton public class MyDataSource { MyDao mMyDao; @Inject public MyDataSource(@NonNull MyDao myDao) { mMyDao = myDao; } } 

Repository

 @Singleton public class MyRepository { private final MyDataSource mMyDataSource; @Inject MyRepository(@Local MyDataSource myDataSource) { mMyDataSource = myDataSource; } } 

ApplicationComponent

 @Singleton @Component(modules = {AndroidSupportInjectionModule.class, ApplicationModule.class, DatabaseModule.class}) public interface ApplicationComponent extends AndroidInjector<MyApp> { @Component.Builder interface Builder { @BindsInstance Builder application(Application application); ApplicationComponent build(); } } 

application

 public class MyApp extends DaggerApplication { private ApplicationComponent mApplicationComponent; @Override protected AndroidInjector<? extends DaggerApplication> applicationInjector() { mApplicationComponent = DaggerApplicationComponent.builder().application(this).build(); return mApplicationComponent; } } 
+5
source share
1 answer

I just saw an example of Google and how they did it, and I realized that you need to fix it:

 @Module public class DatabaseModule { @Provides @Singleton MyDatabase provideMyDatabase(Application context) { return Room.databaseBuilder(context.getApplicationContext(), MyDatabase.class, "MyDB").build(); } @Provides @Singleton MyDao provideMyDao(MyDatabase myDatabase) { return myDatabase.myDao(); } 

}

to check if a database already exists with this code:

  /** * Check whether the database already exists and expose it via {@link #getDatabaseCreated()} */ private void updateDatabaseCreated(final Context context) { if (context.getDatabasePath(DATABASE_NAME).exists()) { setDatabaseCreated(); } } private void setDatabaseCreated(){ mIsDatabaseCreated.postValue(true); } public LiveData<Boolean> getDatabaseCreated() { return mIsDatabaseCreated; } 

Anyway, here is a link to a google github example: https://github.com/googlesamples/android-architecture-components/blob/master/BasicSample/app/src/main/java/com/example/android/persistence/db /AppDatabase.java

+1
source

Source: https://habr.com/ru/post/1273678/


All Articles