LiveData.getValue() returns null with Room

前端 未结 7 876
无人及你
无人及你 2020-12-05 13:16

Java POJO Object

public class Section {

    @ColumnInfo(name=\"section_id\")
    public int mSectionId;

    @ColumnInfo(name=\"section_name\")
    public S         


        
7条回答
  •  遥遥无期
    2020-12-05 14:06

    I would suggest to create another query without LiveData if you need to synchronously fetch data from database in your code.

    DAO:

    @Query("SELECT COUNT(*) FROM section")
    int countAllSections();
    

    ViewModel:

    Integer countAllSections() {
        return new CountAllSectionsTask().execute().get();
    }
    
    private static class CountAllSectionsTask extends AsyncTask {
    
        @Override
        protected Integer doInBackground(Void... notes) {
            return mDb.sectionDAO().countAllSections();
        }
    }
    

提交回复
热议问题