android-房间数据库未创建架构组件

时间:2019-04-29 05:11:12

标签: android android-room android-architecture-components android-viewmodel android-mvvm

我正在使用android体系结构组件和MVVM,正在为离线模式使用空间,这是我进行连接的代码:

    @Database(entities = arrayOf(Cat::class), version = 1)
abstract class DbConnection : RoomDatabase() {
    abstract fun CategoryDao(): CategoryDao

companion object {
    private var INSTANCE: DbConnection? = null

    fun getInstance(context: Context): DbConnection? {
        if (INSTANCE == null) {
            synchronized(DbConnection::class) {
                INSTANCE = Room.databaseBuilder(
                    context.getApplicationContext(),
                    DbConnection::class.java, Const.db_Name
                ).build()
            }
        }
        return INSTANCE
    }

    fun destroyInstance() {
        INSTANCE = null
    }
}

这是我的DAO课:

    @Dao
interface CategoryDao{
    @Query("select * from $db_categoryTable")
    fun getCatOffline():Single<List<Cat>>

    @Insert(onConflict = REPLACE)
    fun insert(cat:Cat)
}

这是我的Cat课:

        @Entity(tableName = Const.db_categoryTable)
    data class Cat(
        @PrimaryKey(autoGenerate = true)
        @SerializedName("id")
        @Expose
        val id: Int,
        val date_update: String,
        val name: String,
        val numCards: Int,
        val uid: String
    )

and this is my model class :

    class CategoryModel(
    private val netManager: NetManager,
    private val sharedPrefManager: SharedPrefManager
) {
    var dateChanges: String = "null";

    private lateinit var categoryDao: CategoryDao
    private lateinit var dbConnection: DbConnection

    fun getCats(): MutableLiveData<MutableList<Cat>> {
        dbConnection= DbConnection.getInstance(MyApp.INSTANCE)!!
        categoryDao=dbConnection.CategoryDao()

        if (netManager.isConnected!!) {
            return  getCatsOnline();
        } else {
            return getCatsOffline();
        }
    }

    private fun getCatsOffline(): MutableLiveData<MutableList<Cat>> {
        Log.v("this","offline ");

        var list = MutableLiveData<MutableList<Cat>>();

        categoryDao.getCatOffline()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                {subccess->
                    list+=subccess
                },{
                    error->
                    Log.v("This",error.localizedMessage)
                }
            )
        return list
    }

    private fun getCatsOnline(): MutableLiveData<MutableList<Cat>> {
        Log.v("this","online ");
        var list: MutableLiveData<MutableList<Cat>> = MutableLiveData()
        val getCats = ApiConnection.client.create(Category::class.java)

        getCats.getCats(sharedPrefManager.getUid(), dateChanges)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                { success ->
                    list += success.cats
                }, { error ->
                    Log.v("this", "ErrorGetCats " + error.localizedMessage);
                }
            )

        return list;
    }

    operator fun <T> MutableLiveData<MutableList<T>>.plusAssign(values: List<T>) {
        val value = this.value ?: arrayListOf()
        value.addAll(values)
        this.value = value
    }

}
如您所见,

模型类具有互联网连接时分为两个部分,它联机,否则,脱机。

问题是,它创建了数据库,但是当我下载并浏览它时,它没有任何表和内容,它只是一个空数据库,因此在脱机模式下它没有任何价值。

此代码有什么问题?

1 个答案:

答案 0 :(得分:1)

查看上面的代码,看来您没有在房间中插入任何数据。您还应该在getCatsOnline()中的Room中插入数据,以便在访问Room时能够获取数据。