会议室-未触发LiveData [英] Room - LiveData not triggered

查看:111
本文介绍了会议室-未触发LiveData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Room数据库&LiveData.我有ViewModels,其中保存了从dao获得的LiveData.如果我更新了 Transaction ,则可以正常观察到 LiveData< List< List< Transaction>> ,但根本不会观察到 LiveData< Transaction> .那怎么可能?我在做什么错了?

 公共抽象类Dao {@Query("SELECT * FROM TRANSACT WHERE Deleted =:value")公共抽象LiveData< List< Transaction>>allTransactions(boolean value);@Query("SELECT * FROM transact WHERE guid =:guid AND Deleted =:value")公共抽象LiveData< Transaction>getTransaction(String guid,布尔值);@Update(onConflict = OnConflictStrategy.REPLACE)受保护的抽象void updateTransaction(Transaction transaction);} 

有一个类似的问题,它提到使用非 @Singleton 注释的类时的Dagger并发症,不幸的是,即使我使用Dagger,这也不是我的问题.

解决方案

问题出在 AppCompatActivity 内部,该应用程序包含 ViewModel LiveData ./p>

仅为Fragment调用了LiveData观察器,没有为Activity调用.我在实现了 LifecycleOwner 接口的情况下使用了 AppCompatActivity ,但正确的做法是实现 LifecycleRegistryOwner .

信用: https://issuetracker.google.com/issues/63764057

I'm trying to work with Room database & LiveData. I have ViewModels which holds LiveData they got from dao. If I update a Transaction, then LiveData<List<Transaction>> is observed ok, but LiveData<Transaction> is not observed at all. How is that possible? What am i doing wrong?

public abstract class Dao {
    @Query("SELECT * FROM transact WHERE deleted = :value")
    public abstract LiveData<List<Transaction>> allTransactions(boolean value);

    @Query("SELECT * FROM transact WHERE guid = :guid AND deleted = :value ")
    public abstract LiveData<Transaction> getTransaction(String guid, boolean value);

    @Update(onConflict = OnConflictStrategy.REPLACE)
    protected abstract void updateTransaction(Transaction transaction);
}

There is similar issue, which mentions Dagger complications when non @Singleton annotated class was used, that's unfortunately not my problem, even if i use Dagger.

解决方案

The problem was inside AppCompatActivity which holds ViewModel with LiveData.

LiveData observer has been called only for Fragment, not for Activity. I used AppCompatActivity with LifecycleOwner interface implemented, but correct is to implement LifecycleRegistryOwner.

Credits: https://issuetracker.google.com/issues/63764057

这篇关于会议室-未触发LiveData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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