Pre-populate Room database fetching data from different API endpoints using LiveData

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Pre-populate Room database fetching data from different API endpoints using LiveData



I have to display a list of dogs (their names) and also for each dog to display their owner's name.



But The problem is I have to fetch data from two different endpoints:



Endpoint 1: Fetch a list of person



JSON Response :


[
"id" : 1,
"name": "Mike"
,

"id" : 1,
"name": "Joan"
]



Endpoint 2: Fetch list of dog



Json Response :


[
"id" : 1,
"userId" : 1
"name": "Doggity"
,

"id" : 2,
"userId" : 1
"name": "Doggo"
,

"id" : 3,
"userId" : 2
"name": "Doggy"
]



The moment I need to display the list of dogs I need to already had the list of their owner. For that reason, I have to pre-populate the room database with those data (fetching from the server).



But I don't know how to that because the LiveData only fetches data from one endpoint. I mean, If I need to list dogs (Endpoint 2), I do:


mDogListViewModel.getDogs()
.observe(this, this::response);



And this is what the repository do:


public LiveData<Response<List<Dog>>> getListDogs()
return new NetworkBoundResource<List<Dog>,List<Dog>>(mAppExecutors)

@Override
protected void saveCallResult(@NonNull List<Dog> item)
mDogDao.insertDogs(item);


@Override
protected boolean shouldFetch(@Nullable List<Dog> data)

@NonNull
@Override
protected LiveData<List<Dog>> loadFromDb()
return mDogDao.findAll();


@NonNull
@Override
protected LiveData<ApiResponse<List<Dog>>> createCall()
return mService.getDogs();

.asLiveData();



But I need to get the owner's name. So I need to fetch Endpoint 1 data BEFORE fetching Endpoint 2 data.



What do I need?



Pre-populate data fetching and saving into my room Database a table with users and a table with dogs so by the time I need to list dos I've already had their owners.



What is the best practice to pre-populate my Room database? Do I need a LiveData to prepopulate my Room Database?



Thanks.





I am so sorry that I could not give you solution codes, but I have another resource to help you to solve your problem. I believe this link will help you: youtu.be/MfHsPGQ6bgE?t=10m20s It does not matter you solved the problem or not, please tell me about the result. Good luck!
– Mirjalal Talishinski
Aug 12 at 21:13





Thanks but it doesn't solve the problem.
– Faustino Gagneten
Aug 12 at 21:35









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS