@Insert后Android Room擦除表

时间:2018-04-19 15:19:33

标签: android kotlin android-architecture-components

我有BD聊天消息,当我在coroutine中插入项目时(例如,在3到3秒内10-20项) - 没关系,他们进去了,如果我打电话给getAllChatMessages()我会看到它们,但是当我在10秒钟后拨打getAllChatMessages()时 - 它会返回空列表,我没有看到任何错误或其他内容而且我没有得到它,为什么它们是添加到表格中,我可以看到它们,但几秒钟后 - 表格为空

fun insert(message:ChatMessageRoomEntity) {
    launch {
        chatMessagesDao.putChatMessage(message)
        Log.d("DBG", "size is " + chatMessagesDao.getAllChatMessages().size) //here I can see that size has increased
    }
}

@Dao
interface ChatMessagesDao {

    @Query("SELECT * FROM chatMessages")
    fun getAllChatMessages(): List<ChatMessageRoomEntity>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun putChatMessage(message: ChatMessageRoomEntity)

}

DATABSE

@Database(entities = arrayOf(
        ChatMessageRoomEntity::class,
        UserRoomEntity::class),
        version = 8)
@TypeConverters(DateTypeConverter::class)
abstract class DataBase : RoomDatabase() {

    abstract fun chatMessagesDao(): ChatMessagesDao
    abstract fun usersDao(): UsersDao
    companion object {

        @VisibleForTesting
        val DATABASE_NAME = "DataBase.db"

        @Volatile
        private var INSTANCE: DataBase? = null

        fun getInstance(context: Context): JCDataBase =
                INSTANCE ?: synchronized(this) { return INSTANCE ?: buildDatabase(context).also { INSTANCE = it } }

        private fun buildDatabase(context: Context) =
                Room.databaseBuilder(context.applicationContext,
                        DataBase::class.java, DATABASE_NAME)
                        .fallbackToDestructiveMigration()
                        .build()
    }

}

消息实体

@Entity(tableName = "chatMessages")
data class ChatMessageRoomEntity(@PrimaryKey
                             @ColumnInfo(name = "messageId")
                             var messageId: Long = 0,
                             @ColumnInfo(name = "message")
                             @Expose
                             var message: String = " ",
                             @ColumnInfo(name = "created_at")
                             @Expose
                             var created_at: Date? = null,
                             @ColumnInfo(name = "user")
                             @Expose
                             var userId: Long? = null,
                             @ColumnInfo(name = "status")
                             @Expose
                             var status: String? = null)

我开始在循环中添加项目并每0.5秒检查一次数据库内容,这就是我所拥有的:

04-20 06:49:11.494 D/DBG_CHT_DB: chat messages DAO has [] elements
04-20 06:49:11.992 D/DBG_CHT_DB: chat messages DAO has [] elements
04-20 06:49:12.152 D/DBG_CHT_DB: added one message 12087
04-20 06:49:12.159 D/DBG_CHT_DB: added one message 186392
04-20 06:49:12.168 D/DBG_CHT_DB: added one message 166261
04-20 06:49:12.199 D/DBG_CHT_DB: added one message 57174
04-20 06:49:12.215 D/DBG_CHT_DB: added one message 193400
04-20 06:49:12.229 D/DBG_CHT_DB: added one message 207086
04-20 06:49:12.244 D/DBG_CHT_DB: added one message 207121
04-20 06:49:12.257 D/DBG_CHT_DB: added one message 207204
04-20 06:49:12.268 D/DBG_CHT_DB: added one message 207244
04-20 06:49:12.282 D/DBG_CHT_DB: added one message 207254
04-20 06:49:12.296 D/DBG_CHT_DB: added one message 207284
04-20 06:49:12.309 D/DBG_CHT_DB: added one message 207299
04-20 06:49:12.331 D/DBG_CHT_DB: added one message 207324
04-20 06:49:12.356 D/DBG_CHT_DB: added one message 207404
04-20 06:49:12.384 D/DBG_CHT_DB: added one message 207459
04-20 06:49:12.416 D/DBG_CHT_DB: added one message 207499
04-20 06:49:12.500 D/DBG_CHT_DB: chat messages DAO has [12087, 57174, 166261, 186392, 193400, 207086, 207121, 207204, 207244, 207254, 207284, 207299, 207324, 207404, 207459, 207499] elements
04-20 06:49:12.845 D/DBG_CHT_DB: added one message 207644
04-20 06:49:12.863 D/DBG_CHT_DB: added one message 207529
04-20 06:49:12.878 D/DBG_CHT_DB: added one message 207539
04-20 06:49:12.901 D/DBG_CHT_DB: added one message 207544
04-20 06:49:12.938 D/DBG_CHT_DB: added one message 207579
04-20 06:49:12.966 D/DBG_CHT_DB: added one message 207594
04-20 06:49:12.980 D/DBG_CHT_DB: added one message 207629
04-20 06:49:12.996 D/DBG_CHT_DB: added one message 207664
04-20 06:49:13.013 D/DBG_CHT_DB: added one message 207714
04-20 06:49:13.025 D/DBG_CHT_DB: chat messages DAO has [12087, 57174, 166261, 186392, 193400, 207086, 207121, 207204, 207244, 207254, 207284, 207299, 207324, 207404, 207459, 207499, 207529, 207539, 207544, 207579, 207594, 207629, 207644, 207664, 207714] elements
04-20 06:49:13.492 D/DBG_CHT_DB: chat messages DAO has [] elements

注意,在添加message 207714之后 - 它在数据库中,但在此内容消失之后,这些调用之间没有发生任何事情

我也尝试过不那么熟练并且使用了RxJava缓冲区,它看起来像这样

bufferFlowable.buffer(1, TimeUnit.SECONDS, scheduler)
            .subscribe {
                val list = it.flatten()
                if (list.isNotEmpty())
                    chatMessagesDao.putChatMessages(list)
                val listString = list.map { it.messageId }.toString()
                Log.d("DBG_CHT_DB", "added $listString elements")

                val chats = chatMessagesDao.getAllChatMessages()
                val size = chats.map { it.messageId }.toString()
                Log.d("DBG_CHT_DB", "chat messages DAO has $size elements")
            }

这里的结果,对我来说真的很奇怪

04-20 07:26:56.601 D/DBG_CHT_DB: chat messages DAO has [207529, 207539, 207544, 207579, 207594, 207629, 207644, 207664, 207714] elements
04-20 07:26:57.599 D/DBG_CHT_DB: added [] elements
04-20 07:26:57.602 D/DBG_CHT_DB: chat messages DAO has [207529, 207539, 207544, 207579, 207594, 207629, 207644, 207664, 207714] elements
04-20 07:26:58.601 D/DBG_CHT_DB: added [] elements
04-20 07:26:58.606 D/DBG_CHT_DB: chat messages DAO has [207529, 207539, 207544, 207579, 207594, 207629, 207644, 207664, 207714] elements
04-20 07:26:59.600 D/DBG_CHT_DB: added [] elements
04-20 07:26:59.601 D/DBG_CHT_DB: chat messages DAO has [207529, 207539, 207544, 207579, 207594, 207629, 207644, 207664, 207714] elements
04-20 07:27:00.611 D/DBG_CHT_DB: added [12087, 186392, 166261, 57174, 193400, 207086, 207121, 207204, 207244, 207254, 207284, 207299, 207324, 207404, 207459, 207499] elements
04-20 07:27:00.634 D/DBG_CHT_DB: chat messages DAO has [12087, 57174, 166261, 186392, 193400, 207086, 207121, 207204, 207244, 207254, 207284, 207299, 207324, 207404, 207459, 207499, 207529, 207539, 207544, 207579, 207594, 207629, 207644, 207664, 207714] elements
04-20 07:27:01.609 D/DBG_CHT_DB: added [207644, 207529, 207539, 207544, 207579, 207594, 207629, 207664, 207714] elements
04-20 07:27:01.611 D/DBG_CHT_DB: chat messages DAO has [207529, 207539, 207544, 207579, 207594, 207629, 207644, 207664, 207714] elements
04-20 07:27:02.600 D/DBG_CHT_DB: added [] elements
04-20 07:27:02.602 D/DBG_CHT_DB: chat messages DAO has [207529, 207539, 207544, 207579, 207594, 207629, 207644, 207664, 207714] elements
04-20 07:27:03.601 D/DBG_CHT_DB: added [] elements
04-20 07:27:03.604 D/DBG_CHT_DB: chat messages DAO has [207529, 207539, 207544, 207579, 207594, 207629, 207644, 207664, 207714] elements

看起来事务不成功 - DB在事务之前具有相同的元素,我找不到查看错误或捕获异常的方法 如果我将缓冲时间增加到3秒 - 没关系,所有项目都保留在数据库中而没有消失,我几乎可以肯定它必须完成多线程和事务错误

0 个答案:

没有答案