Observer not triggered on room database change

泄露秘密 提交于 2021-02-09 07:55:43

问题


I am trying to update a RecyclerView when a room database is changed. However the onChanged method of the observer defined in the MainActivity does not get called, when a change to the database occurs. If I let the DAO return LiveData instead of a List<> and use LiveData in my ViewModel it works as intended. However I need MutableLiveData as I want to change my query to the database during runtime.

My setup:

MainActivity.java

BorrowedListViewModel viewModel = ViewModelProviders.of(this).get(BorrowedListViewModel.class);

    viewModel.getItemAndPersonList().observe(MainActivity.this, new Observer<List<BorrowModel>>() {
        @Override
        public void onChanged(@Nullable List<BorrowModel> itemAndPeople) {
            recyclerViewAdapter.addItems(itemAndPeople);
        }
    });

BorrowedListViewModel.java

public class BorrowedListViewModel extends AndroidViewModel {

    private final MutableLiveData<List<BorrowModel>> itemAndPersonList;

    private AppDatabase appDatabase;

    public BorrowedListViewModel(Application application) {
        super(application);

        appDatabase = AppDatabase.getDatabase(this.getApplication());

        itemAndPersonList = new MutableLiveData<>();

        itemAndPersonList.setValue(appDatabase.itemAndPersonModel().getAllBorrowedItems());
    }

    public MutableLiveData<List<BorrowModel>> getItemAndPersonList() {
        return itemAndPersonList;
    }


    //These two methods should trigger the onChanged method
    public void deleteItem(BorrowModel bm) {
        appDatabase.itemAndPersonModel().deleteBorrow(bm);
    }

    public void addItem(BorrowModel bm){
        appDatabase.itemAndPersonModel().addBorrow(bm);
    }

}

BorrowModelDao.java

public interface BorrowModelDao {

    @Query("select * from BorrowModel")
    //Using LiveData<List<BorrowModel>> here solves the problem
    List<BorrowModel> getAllBorrowedItems();

    @Query("select * from BorrowModel where id = :id")
    BorrowModel getItembyId(String id);

    @Insert(onConflict = REPLACE)
    void addBorrow(BorrowModel borrowModel);

    @Delete
    void deleteBorrow(BorrowModel borrowModel);

}

AppDatabase.java

public abstract class AppDatabase extends RoomDatabase {

    private static AppDatabase INSTANCE;

    public static AppDatabase getDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "borrow_db")
                            .allowMainThreadQueries()
                            .build();
            }
            return INSTANCE;
    }

    public static void destroyInstance() {
        INSTANCE = null;
    }

    public abstract BorrowModelDao itemAndPersonModel();

}

来源:https://stackoverflow.com/questions/48852692/observer-not-triggered-on-room-database-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!