Android Room:更新插入的LiveData回调? [英] Android Room : LiveData callback of update insert?

查看:492
本文介绍了Android Room:更新插入的LiveData回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有CRUD功能的简单DAO

FeedEntryDAO.java

FeedEntryDAO.java

@Dao
public interface FeedEntryDAO {

  @Query("SELECT * FROM feedEntrys")
  LiveData<List<FeedEntry>> getAll();

  @Query("SELECT * FROM feedEntrys WHERE uid = :uid LIMIT 1")
  LiveData<FeedEntry> findByUid(int uid);

  @Insert
  void insertAll(FeedEntry... feedEntries);

  @Delete
  void delete(FeedEntry feedEntry);

  @Update
  int update(FeedEntry feedEntry);

}

对于select,可以返回LiveData类型.

For the select , it is okay to return the LiveData type.

在活动"中,代码​​很适合选择

Inside the Activity the code is pretty for the selection

viewModel.getFeedEntrys().observe(this,entries -> {...});

但是,当我尝试插入,更新,删除数据时.该代码似乎有点丑陋,并且每次都会创建一个asynctask.

However, when I try to insert, update , delete the data. The code seems a little bit ugly and also create a asynctask every time.

new AsyncTask<FeedEntry, Void, Void>() {
                @Override
                protected Void doInBackground(FeedEntry... feedEntries) {
                  viewModel.update(feedEntries[0]);
                  return null;
                }
}.execute(feedEntry);

我对此有2个问题:

  1. 我可以使用LiveData包装删除,插入,更新功能吗?
  2. 维护此类asynctask类的更好方法,以便进行删除,插入,更新?

感谢任何建议.谢谢.

推荐答案

  1. 我可以使用LiveData封装删除",插入",更新"呼叫吗?

不,您不能.我为问题写了一个答案.原因是,LiveData用于通知更改.插入,更新,删除不会触发更改.它将返回已删除的行,插入的ID或受影响的行.即使看起来很恐怖,也不要将LiveData包裹在您的内容中.无论如何,在调用周围使用诸如Single之类的东西来让该操作在RX-Java操作上触发和操作是有意义的.

No, you can't. I wrote an answer to the issue. The reason is, that LiveData is used to notify for changes. Insert, Update, Delete won't trigger a change. It will return the deleted rows, the inserted ID or the affected rows. Even if it looks horrible it makes sense not having LiveData wrapped around your stuff. Anyway, it would make sense to have something like Single around the calls to let the operation triggered and operated on a RX-Java operation.

如果要触发这些呼叫,则会在选择查询中观察到,该查询通知LiveData一个人您已更新,插入或删除了一些/任何数据.

If you want to trigger those calls, you observe on a selection query which notify your LiveData onec you have updated, inserted or deleted some/any data.

  1. 维护此类asynctask类的更好方法,以便进行删除,插入,更新?

查看示例后,您似乎误用了(Model/View/)ViewModel-Pattern.您永远都不应在视图中访问您的存储库.我不确定您是否正在执行此操作,因为它在示例中不可见.无论如何,在观察LiveData并获得结果之后,无需将更新数据包装在AsyncTask中的viewModel中.这意味着,您应该始终照顾好

After looking at your example it looks like that you misuse the (Model/View/)ViewModel-Pattern. You should never access your repository in your view. I'm not sure if you'r doing this because its not visible in your sample. Anyway, after observing your LiveData and getting a result, there's no need to wrap the updating of data inside your viewModel in an AsyncTask. That means, that you should alway take care of

a)查看<-> viewmodel<->存储库,而不查看<->存储库并查看<-> viewmodel

a) view <-> viewmodel <-> repository and not view <-> repository and view <-> viewmodel

b)不要尝试使用不需要的线程.您默认情况下(如果未使用@MainThread注释)在后台线程(@WorkerThread)上观察LiveData,并在ui线程(@MainThread)中获取该值.

b) don't try to use threads which are not needed. You observe LiveData on a Background Thread (@WorkerThread) by default (if not annotated with @MainThread) and get the value in the ui-thread (@MainThread).

这篇关于Android Room:更新插入的LiveData回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆