Android Room-通过自动生成获取新插入行的ID-MVVM版本 [英] Android Room - Get the id of new inserted row with auto-generate - MVVM Version

查看:65
本文介绍了Android Room-通过自动生成获取新插入行的ID-MVVM版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如此处所述 Android Room-通过自动生成获取新插入行的ID ,可以从新插入的对象获取ID.

As discussed here Android Room - Get the id of new inserted row with auto-generate it is possible to get the ID from the newly inserted object.

但是如何以MVVM模式从DAO到我的Activity类一直获取此ID?

关于此的一些答案建议使用rxjava和类似的东西:
返回Single.fromCallable< Long>{recipesDao.insertManual(cookingRecipes)}
但是,仅仅为了一个方法调用而实现两个完整的Gradle依赖关系是否有意义?

Some answers on there suggest using rxjava and something like this:
return Single.fromCallable<Long> { recipesDao.insertManual(cookingRecipes) }
But does it make sense to implement two whole Gradle dependencies just to have one method call?

没有办法使这种结构与我当前的结构兼容吗?

Isn't there a way to make this work with my current structure?

活动-ViewModel-存储库(-AsyncTask)-道

Activity - ViewModel - Repository ( - AsyncTask) - Dao


唯一的问题是从insert语句获取返回值,该插入值是从 AsyncTask :

private static class UpdateChallengeInfosAsyncTask extends AsyncTask<ChallengeInfos, Void, Void> {

        private ChallengeInfosDao mAsyncTaskDao;

        UpdateChallengeInfosAsyncTask(ChallengeInfosDao dao) {
            mAsyncTaskDao = dao;
        }

        @Override
        protected Void doInBackground(ChallengeInfos... challengeInfos) {
            mAsyncTaskDao.insert(challengeInfos[0]);
            return null;
        }
    }

也许像这样的 onPostExecute() + Interface 组合可能吗?
如何获取由于AsyncTask是一个单独的类,导致OnPostExecute()返回主活动的结果?

Maybe it's possible with a onPostExecute() + Interface combo like here?
How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

推荐答案

好的,所以我最终要做的是,看来效果很好:

Okay so what I ended up doing and what seems to be working pretty well is this:

基本上使用此答案中的结构:
https://stackoverflow.com/a/12575319/1972372

Basically used the structure from this answer:
https://stackoverflow.com/a/12575319/1972372

并在我的 ViewModel 中使用 MutableLiveData 对象实现了 AsynTask 回调接口,以便Activity在开始时调用一次并挂起直到更新一次:

And implemented the AsynTask callback-interface in my ViewModel with a MutableLiveData object for the Activity to call once in the beginning and hang on to until it is updated once:


public class ChallengeViewModel extends AndroidViewModel implements ChallengesRepository.UpdateChallengeInfosAsyncTask.AsyncResponse {
    
    private MutableLiveData<Integer> insertedChlgInfoID;

....
....

    public void insertChallengeInfos(ChallengeInfos challengeInfos) {
        mRepository.insertChallengeInfos(challengeInfos, this);
    }

    public LiveData<Integer> getInsertedChlgInfoID() {
        return insertedChlgInfoID;
    }

    @Override
    public void processFinish(Long result) {
        insertedChlgInfoID.setValue(Ints.checkedCast(result));
    }
}

我了解在某些情况下 AsyncTask 可能被称为错误的原因,但是我认为在我的情况下它确实可以正常工作,并且没有任何潜在的内存泄漏或类似风险,因为这只是非常基本的写操作.重写整个逻辑似乎对我的情况没有任何好处.

I understand the reasons why AsyncTask may be called bad in certain scenarios, but I think in my case it really works fine and doesn't really have any potential risks of memory leaks or similar, since it's only very basic write operations. And rewriting the whole logic doesn't seem to be of any benefit for my case.

这篇关于Android Room-通过自动生成获取新插入行的ID-MVVM版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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