简体   繁体   English

房间中的查询构造(Android Kotlin)

[英]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我正在研究 android ROOM + Kotlin(刚开始)我想进行一个更新表行的查询,但我无法找到关于如何从查询中的参数访问值的定义

@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.我想访问注释 object 中的描述。 like note.description how to do that inside the query !!喜欢 note.description 如何在查询中做到这一点! Any help will good!任何帮助都会很好!

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 将 :minAge 绑定参数与 minAge 方法参数匹配。 Room performs the match using the parameter names. Room 使用参数名称执行匹配。 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.其他选项是使用Raw query ,但这对于用例来说是不必要的。

Understand why Room doesn't allow object references了解 Room 为何不允许 object 引用

Mapping relationships from a database to the respective object model is a common practice and works very well on the server side. 从数据库映射关系到相应的 object model 是一种常见的做法,并且在服务器端运行良好。 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.但是,在客户端,这种延迟加载是不可行的,因为它通常发生在 UI 线程上,并且在 UI 线程中查询磁盘上的信息会产生重大的性能问题。 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. UI 线程通常有大约 16 毫秒的时间来计算和绘制 Activity 的更新布局,因此即使查询只需要 5 毫秒,您的应用程序仍然可能会超时绘制框架,从而导致明显的视觉故障。 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.但是,如果您不使用延迟加载,您的应用获取的数据会超出其需要,从而产生 memory 消耗问题。

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.开发人员通常决定在他们的应用程序和 UI 之间共享 model。 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.但是,此解决方案不能很好地扩展,因为随着 UI 随时间变化,共享的 model 会产生开发人员难以预测和调试的问题。

from docs: https://developer.android.com/training/data-storage/room/referencing-data来自文档: https://developer.android.com/training/data-storage/room/reference-data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM