房间 - 未触发 LiveData

Room - LiveData not triggered

我正在尝试使用 Room 数据库和 LiveData。 我有 ViewModels,其中包含他们从 dao 获得的 LiveData。 如果我更新 Transaction,那么可以观察到 LiveData<List<Transaction>>,但根本不会观察到 LiveData<Transaction>。这怎么可能?我做错了什么?

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);
}

有类似的问题,当使用非 @Singleton 注释 class 时提到 Dagger 并发症,不幸的是这不是我的问题,即使我使用 Dagger。

问题出在 AppCompatActivity 内部,其中包含 ViewModelLiveData

LiveData 观察器仅针对片段调用,未针对 Activity。 我使用 AppCompatActivity 实现了 LifecycleOwner 接口,但正确的是实现 LifecycleRegistryOwner.

学分:https://issuetracker.google.com/issues/63764057

在新版本1.0.0 Alpha 9-1中,Android开发者发布说明公布

This is a major release where core lifecycle artifacts (runtime, common) and arch core (common) reach to stable version 1.0.0.

Along with this change, Support Library 26.1.0 now depends on these libraries. Both AppCompatActivity and Support Fragment now implement the LifecycleOwner interface.

This release also depends on Support Library 26.1.0 to take advantage of the new integration.

AppCompatActivity 和 Support Fragment 现在都应该实现 LifecycleOwner 接口