Solution 1 :
Please check your the Room
library version you use. In Room 2.1 and higher, you can use the suspend
keyword to make your DAO queries asynchronous using Kotlin coroutines:
dependencies {
def room_version = "2.4.2" // current stable version
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
...
}
Problem :
So I am working on Unit 5 Forge app project from Android Basics in Kotlin, in the DAO :
// TODO: implement a method to insert a Forageable into the database
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(forageable: Forageable)
// TODO: implement a method to update a Forageable that is already in the database
@Update
suspend fun update(forageable: Forageable)
// TODO: implement a method to delete a Forageable from the database.
@Delete
suspend fun delete(forageable: Forageable)
Compiling will generate this “error: Not sure how to handle insert method’s return type”. It happened for all Insert, Update and Delete.
If I removed the “suspend” keyword, then the errors will go away. But app will crash because “ Cannot access database on the main thread since it may potentially lock the UI for a long period of time.”
There is anther lab called Inventory app in the same Unit 5 that used the same design and it was working fine with the “suspend” keyword.
The Forage app was modeled after the Inventory app. On both projects, I used all the same dependencies and versions.
Any suggestions?
Edit: It is using coroutines.
private fun updateForage(forageable: Forageable) {
viewModelScope.launch {
forageableDao.update(forageable)
}
}
Also have tried this:
@Delete
suspend fun delete(forageable: Forageable): Long
Comments
Comment posted by Stefan de Kraker
yes how do you call the functions? As @AymenBenSalah points out, you should be using corountines. When you mak a call from you activity you can do:
Comment posted by Aymen Ben Salah
Oki did you try to add Long as return type??
Comment posted by EmilyJ
@StefandeKraker: See my edits.
Comment posted by EmilyJ
@AymenBenSalah: See my edits.
Comment posted by ianhanniballake
What are you dependencies? Where do you declare your dependency on