I am looking for recommendation in handling below use case w.r.t. LiveData handling: ViewModel invokes Repository for LiveData (e.g. products catalog). Repository checks from LocalDataSource(Room) first but if data not available, invokes RemoteDataSource(REST API).
Questions:
1) As per below comment from yigit here, Repository can't get LiveData availability status in Room until it subscribes as observer. So even when data is avaiable, I get null in response. "LiveData is to watch the data and distribute it to the observers. It won't calculate the value until an active observer is added."
Since Repository hides data sources to clients, it is repository's responsibility to check in Room and then pull from Remote Source. How can it check for data availability in Room ?
2) Since data returned by RemoteDataSource is not LiveData type, what should be done so that repository eventually returns LiveData to ViewModel ?
Should Repository first insert data (from RemoteDataSource) into Room first and then query Room so it returns LiveData ? It looks quite expensive operation as there Room is queried twice in the process. Please advise . Thanks !