应用程序重启后,房间空Db

时间:2017-11-27 09:28:20

标签: java android dagger-2 android-room dagger

我正在尝试将Room用于我的新应用。但每当我重新启动我的应用程序时,我都无法检索旧数据。看起来我的应用程序每次都在创建一个新的数据库实例,但我不确定为什么。这是我的基于MVP模式的类。

编辑:我刚刚再次检查,我可以看到实体的自动生成的Id(MyModel)没有重置为1,但是当我检索数据时,我只在该会话中插入了集合。

@Dao
public interface MyDao {

      @Insert(onConflict = OnConflictStrategy.REPLACE)
      List<Long> insertModels(List<MyModel> models);

      @Query("SELECT * FROM MyModel")
      List<MyModel> getModels();
}

数据库

@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();
    }
}

数据来源

@Singleton
public class MyDataSource {

    MyDao mMyDao;

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

存储库

@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();
    }
}

应用

public class MyApp extends DaggerApplication {

    private ApplicationComponent mApplicationComponent;

    @Override
    protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
        mApplicationComponent = DaggerApplicationComponent.builder().application(this).build();
        return mApplicationComponent;
    }
}

1 个答案:

答案 0 :(得分:2)

我刚看到谷歌示例以及他们是如何做到的,我意识到你需要修复:

@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();
}

}

使用以下代码检查数据库是否已存在:

  /**
 * 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;
}

无论如何这里是github谷歌样本的链接: https://github.com/googlesamples/android-architecture-components/blob/master/BasicSample/app/src/main/java/com/example/android/persistence/db/AppDatabase.java