LiveData.getValue() returns null with Room

前端 未结 7 873
无人及你
无人及你 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 13:57

    if sections.getValue() is null I have to call api for data and insert in into the database

    You can handle this at onChange method:

    sections.observe(this, new Observer>() {
        @Override
        public void onChanged(@Nullable List
    sections){ if(sections == null || sections.size() == 0) { // No data in your database, call your api for data } else { // One or more items retrieved, no need to call your api for data. } } });

    But you should better put this Database/Table initialization logic to a repository class. Check out Google's sample. See DatabaseCreator class.

提交回复
热议问题