Peter
Peter

Reputation: 1149

Query construct in room (Android Kotlin)

I am working on android ROOM + Kotlin (Just started) I want to make a query which update the row of my table, but I am not able to find a definition on how to access the value from the parameter inside the query

@Query("UPDATE note_table SET description = :description, title= :title, priority = :priority WHERE id =:id")
    fun updateNote(note : Notes)

I want to access the description from note object. like note.description how to do that inside the query !! Any help will good!

Upvotes: 0

Views: 104

Answers (2)

ijk
ijk

Reputation: 11

Understand why Room doesn't allow object references

Mapping relationships from a database to the respective object model is a common practice and works very well on the server side. Even when the program loads fields as they're accessed, the server still performs well.

However, on the client side, this type of lazy loading isn't feasible because it usually occurs on the UI thread, and querying information on disk in the UI thread creates significant performance problems. The UI thread typically has about 16 ms to calculate and draw an activity's updated layout, so even if a query takes only 5 ms, it's still likely that your app will run out of time to draw the frame, causing noticeable visual glitches. The query could take even more time to complete if there's a separate transaction running in parallel, or if the device is running other disk-intensive tasks. If you don't use lazy loading, however, your app fetches more data than it needs, creating memory consumption problems.

Object-relational mappings usually leave this decision to developers so that they can do whatever is best for their app's use cases. Developers usually decide to share the model between their app and the UI. This solution doesn't scale well, however, because as the UI changes over time, the shared model creates problems that are difficult for developers to anticipate and debug.

from docs : https://developer.android.com/training/data-storage/room/referencing-data

Upvotes: 0

mightyWOZ
mightyWOZ

Reputation: 8315

That is not possible, as docs specify

@Query("SELECT * FROM user WHERE age > :minAge")

When this query is processed at compile time, Room matches the :minAge bind parameter with the minAge method parameter. Room performs the match using the parameter names. If there is a mismatch, an error occurs as your app compiles.

Other option is using Raw query, but that is needlessly complected for the use case.

Upvotes: 1

Related Questions