0

If I understand it correctly when there is no active observer for a LiveData object, it doesn’t get changes of the source anymore, for example, it doesn’t require to get changes from SQLite (LiveData in Dao). If again one of its observers becomes active, does it get the data from the source first or it waits for next changes on the source to become updated?

Edit: Considering the comments, I think I should explain my question more precisely. Indeed, my question is about when LiveData itself becomes active. For example, I have following Dao:

 @Dao
public interface SampleDao {
…

@Query("SELECT * FROM sampleTable where createdAt > :ceatedAt")
LiveData<List<sampleEntity>> newSampleEntities(Date ceatedAt);
…

}

And its returned LiveData from newSampleEntities has just one observer, namely activity1. Suppose that sampleEntity1 is inserted in sampleTable. Consequently, Livedata gets it through a query and informs activity1 about it. If activity1 becomes paused (onPause() is called), the Livedata doesn’t have any active observer anymore. So It becomes inactive and doesn’t requery sampleTable anymore. Suppose that sampleEntity1 is deleted from sampleTable and sampleEntity2 is inserted in it. Since the LiveData doesn’t get changes, it has sampleEntity1 yet. Now, suppose that activity1 becomes resumed and consequently the Livedata becomes active again. I want to know when the LiveData gets updated about changes in sampleTable. Does it requery sampleTable before becoming active?

HS.RI
  • 1
  • 3
  • 1
    Hello HS.RI, welcome to SO. Did you do any research on this? What you're asking, is written in the [front page of the LiveData documentation from Google](https://developer.android.com/topic/libraries/architecture/livedata) where it reads, and I quote: _If a lifecycle becomes inactive, it receives the latest data upon becoming active again. For example, an activity that was in the background receives the latest data right after it returns to the foreground._ Check an [Opposite question](https://stackoverflow.com/questions/49832787/android-livedata-prevent-receive-the-last-value-when-subscribe) – Martin Marconcini Oct 22 '19 at 12:38
  • LiveData is an observable **data holder**. It only notifies any observers if they are in `STARTED` or `RESUMED` state. If an observer. If an observer reaches an active state from an inactive state, LiveData shares **most recently** stored data. [LiveData documentation explains pretty well](https://developer.android.com/topic/libraries/architecture/livedata). – Taseer Oct 22 '19 at 13:11

1 Answers1

-1

Yes new observers get notify from previous LiveData value change.

Squti
  • 4,171
  • 3
  • 10
  • 21
  • thanks for your fast answer. you mean observer gets the last data which LiveData knows and if there is some update from LiveData inactivation till its activation, it is ignored and LiveData doesn't check for new version of data at its activation at all. Am I right? – HS.RI Oct 22 '19 at 12:42
  • I didn't understand the second part of your comment but you could always set new values in LiveData even with no observer and new observers always get last value. @HS.RI – Squti Oct 22 '19 at 13:41