Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – In Kotlin, how to handle insert, update and delete methods’ return type when use with suspend

Posted on November 11, 2022 By

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:

READ  [FIXED] android - mutableStateOf() not assinging new object?
Powered by Inline Related Posts

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

Android Tags:android, android-room, kotlin, kotlin-coroutines

Post navigation

Previous Post: [FIXED] java – is permission needed when choosing image from gallery on android?
Next Post: [FIXED] Android Java: How to get component in a custom view? (Trying to access components via findByViewId and get null)

Related Posts

[FIXED] How to dismiss alert dialog when javascript button is clicked in web-view in android Android
[FIXED] encryption – Minimum API level for using android EncryptedSharedPreference? Android
[FIXED] linux – Android emulator using more memory than it should Android
[FIXED] xml – Placing Android Banner Ads over ScrollView Android
[FIXED] How can I add more android compatibility to my react native bluetooth app? Android
[FIXED] android – How to use sealed class for placeholder values in string resource Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is a good substitute for condensed milk?
  • ¿Como el cuerpo te avisa cuando te va a dar un infarto?
  • What is the luxury brand of Jaguar?
  • Who is Big poppa baseball player?
  • What material are foam runners?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme