【发布时间】:2021-03-18 22:47:35
【问题描述】:
我正在关注 Android 房间教程.. 但与它的要求略有不同。我想在后台运行一项工作..并在那里插入一个数据库项目,而不是像他们拥有的那样使用按钮或启动时。我找不到办法吗?
override fun doWork(): Result {
try {
val appContext = applicationContext
......
val newDataItem = DataItem(param1 = "Title", iso_datetime_msg_received = "RIGHTNOW", alert_message = "HI!!!", uuid = 1231)
// THE LINE BELOW IS WHAT I WANT BUT GET AN ERROR
// Suspend function 'insert' should be called only from a coroutine or another suspend function
CustomRoomDatabase.getDatabase(myContext).dataItemDao().insert(newDataItem)
return Result.success(result)
} catch(error: Throwable) {
Log.i(TAG, "RetrieveDataItemWorker got error:" + error)
return Result.retry()
}
}
【问题讨论】: