简体   繁体   English

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

[英]android - room database not created architecture components

I'm using android architecture component and MVVM , i'm using room for offline mode , this is my code for making the connection: 我正在使用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
    }
}

this is my DAO class: 这是我的DAO课:

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

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

this is my Cat class : 这是我的猫课:

        @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
    }

}

the model class as you can see has two parts when it has an internet connection, it goes online and otherwise, it goes to offline. 如您所见,模型类在具有Internet连接时会分为两个部分,它会联机,否则会脱机。

the problem is, it created the database but when I download and browse it, it doesn't have any table and content in it, it's just an empty database so it doesn't get any value when it goes in offline mode. 问题是,它创建了数据库,但是当我下载并浏览它时,它没有任何表和内容,它只是一个空数据库,因此在脱机模式下它没有任何价值。

what is wrong with this code? 此代码有什么问题?

Looking at your code above, it seems you are not inserting any data in your Room. 查看上面的代码,看来您没有在房间中插入任何数据。 You should also insert data in Room in getCatsOnline() so when you access Room, you would be able to get data. 您还应该在getCatsOnline()中的Room中插入数据,以便在访问Room时能够获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM