数据库更改未触发会议室LiveData

时间:2019-10-07 23:35:18

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

我正在读取注入Dagger的房间数据库并在我的DAO中返回一个LiveData,我最初在实例化该片段时读取该列表,并且它可以正常读取,但是当我在数据库中进行更改时,观察者没有被解雇,为什么是吗?

DatabaseModule

@Module
class DatabaseModule {
    @Singleton
    @Provides
    fun provideDatabase(context: Context): AppDatabase {
        return Room.databaseBuilder(
            context,
            AppDatabase::class.java,
            "CargamosDriver"
        )
            .fallbackToDestructiveMigration()
            .build()
    }

    @Singleton
    @Provides
    fun operationsDao(appDatabase: AppDatabase): OperationsDAO {
        Timber.d("Database Hash: ${appDatabase.hashCode()}")
        return appDatabase.getOperationsDao()
    }
}

OperationsDAO

@Dao
interface OperationsDAO {

    @RawQuery
    fun updatePrice(query: SupportSQLiteQuery): Any

    @Query("SELECT * FROM OPERATIONS")
    fun getOperationsFromTodayAsync(): LiveData<List<OperationPersistence>>

}

ServicesFragmentViewModel

class ServicesFragmentViewModel @Inject
constructor(
    val operationsDAO: OperationsDAO
) : BaseViewModel() {
    var noServices: Boolean = true

    val routes: LiveData<List<Route>> =
        Transformations.map(operationsDAO.getOperationsFromTodayAsync()) { operations ->
            val routes = arrayListOf<Route>()
            val routesMap = hashMapOf<Long, ArrayList<Location>>()

            for (operation in operations) {
                val location = Location(
                    id = operation.id,
                    contactName = operation.contact_name,
                    fullAddress = "",
                    routeID = operation.route,
                    items = operation.items,
                    lat = operation.lat,
                    lng = operation.lng,
                    phone = operation.contact_phone,
                    domain = "",
                    state = operation.state,
                    created = operation.created,
                    price = operation.price
                )

                routesMap.getOrPut(operation.route) {
                    arrayListOf()
                }

                routesMap[operation.route]?.add(location)
            }

            routesMap.forEach { (routeID, operations) ->
                val route = Route(
                    routeID = routeID,
                    ongoing = false,
                    locations = operations
                )
                routes.add(route)
            }

            routes
        }

    @WorkerThread
    fun updatePrice() {
        GlobalScope.launch(Dispatchers.Main) {
            withContext(Dispatchers.IO) {
                var random: Long = (0..100).random().toLong()

                val query = SimpleSQLiteQuery(
                    "UPDATE OPERATIONS SET price = ${random}"
                )
                operationsDAO.updatePrice(query)
            }
        }
    }
}

在我的片段中,我观察到这样的实时数据:

viewModel.routes.observe(this,
            Observer {
                viewModel.noServices = it.isEmpty()
                mainViewModel.switchState.value = mainViewModel.switchState.value
                adapter.addAllItems(it)
            })

即使我直接从片段中观察到Room LiveData,也不需要像这样的Tranformations.map:

viewModel.operationsDAO.getOperationsFromTodayAsync().observeForever(
            Observer {
                Timber.d("DATABASE CHANGED")
            }
        )

它仅在实例化片段时触发一次,然后在我更改数据库时也不会触发,并且我确定数据库已更改,因为如果我手动检查数据库,则值会更改并且如果重新加载用新值填充列表的片段

0 个答案:

没有答案