Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using getValue() on LiveData returns null with Room #44

Closed
idragon81 opened this issue Jun 4, 2017 · 8 comments
Closed

Using getValue() on LiveData returns null with Room #44

idragon81 opened this issue Jun 4, 2017 · 8 comments

Comments

@idragon81
Copy link

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());
@yigit
Copy link
Contributor

yigit commented Jun 8, 2017

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.

@yigit yigit closed this as completed Jun 8, 2017
@codecameo
Copy link

This reference might help you.

@sandeepjoshi19
Copy link

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

@shriomtri
Copy link

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

@yigit
Copy link
Contributor

yigit commented Oct 11, 2018

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

@yigit
Copy link
Contributor

yigit commented Oct 11, 2018

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 ?

@chaiwa-berian
Copy link

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).

@CannibalKush
Copy link

I think that this is a completely valid use case.

I want to sometimes just get the current values as they stand, and sometimes react to their changes. I'm not sure whether this change would have to be at Android.Arch.LiveData or whether it would be on Room, but calling LiveData.getValue() to return a synchronous value would be great.

@vikramKirubaharan
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants