问题
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.
回答1:
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
回答2:
In the new version 1.0.0 Alpha 9-1 , Android Developer released notes announced
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.
Both AppCompatActivity and Support Fragment should be implemented the LifecycleOwner interface now
来源:https://stackoverflow.com/questions/45130873/room-livedata-not-triggered