Architecture-components-samples: Using getValue() on LiveData returns null with Room

Created on 4 Jun 2017  路  8Comments  路  Source: android/architecture-components-samples

I have a POJO set up for use with Room and retrofit

@TypeConverters(DateConverter.class)
@Entity(indices = {@Index("code")})
public class HealthcareCenter {
    @PrimaryKey
    @SerializedName("id")
    private int id;
    @SerializedName("code")
    private String code;
    @SerializedName("name")
    private String name;
    @SerializedName("address")
    private String address;
    @SerializedName("phone")
    private String phone;
    @SerializedName("email")
    private String email;
    @ColumnInfo(name = "visited_date")
    @SerializedName("visited_date")
    private Date visited_date;
    ....  //Getters and setters omitted here
}

And have the following method in my Dao

@Query("select * from HealthcareCenter where code = :code")
LiveData<HealthcareCenter> getCenterbyCode(String code);

However when using

 db.healthcareCenterDao().getCenterByCode(code).getValue()

the method returns null.

As a workaround I am omitting the LiveData when I want to retrieve the value once and want to update it (Using an observer leads to a loop).

Dao

   @Query("select * from HealthcareCenter where code = :code")
   HealthcareCenter getNonObservableCenterbyCode(String code);

Use

 HealthcareCenter healthcareCenter = db.healthcareCenterDao().getNonObservableCenterbyCode(qrCode.getCenter_code());

Most helpful comment

don't return LiveData if you want the value sync.
LiveData is to watch the data and distribute it to the observers. It won't calculate the value until an active observer is added.

All 8 comments

don't return LiveData if you want the value sync.
LiveData is to watch the data and distribute it to the observers. It won't calculate the value until an active observer is added.

This reference might help you.

I have added a observer in activity but still getting null in case of LiveData. and correct value when object is returned without livedata.

I think for the first time it returns null and when data is changed then only it returns the changed value.

if you want to receive sync data from the DB, just use a sync query.

Ok then just call room on a background thread and declare your query to return the data sync.
Why do you even try to use LiveData ?

Ok then just call room on a background thread and declare your query to return the data sync.
Why do you even try to use LiveData ?

We would need to do a sync call to initialize the data and then observe the same LiveData for subsequent changes, just like my authentication scenario I posted on Statckoverflow. At startup I want to load the saved token, and then observe it as the token may be invalidated(by background sync task) or updated by the user when they log-out(logging-out doesn't delete the token as it may still be valid and can be re-used especially if the user is disconnected).

Room always returns Null value while using LiveData but returns proper value when we are not wrapping the value with live data. Kindly help us to resolve this. I tried inserting the new data but it is not reflecting as expected.

Was this page helpful?
0 / 5 - 0 ratings