S
S
Shadrindmitry2019-06-21 18:46:35
Java
Shadrindmitry, 2019-06-21 18:46:35

How to get data from room livedata and update it with retrofit?

Data is stored using Room. At the start of the activity, an observe listener is created, where I receive data from the database. I need to update this data using Retrofit and overwrite it in the database. How to organize it correctly? My problem is that when data is updated and overwritten, the observe listener fires and starts updating again. Cycling occurs. The code:

metcastListViewModel.getMetcastListList().observe(MainActivity.this, new Observer<List<metcast>>() {
            @Override
            public void onChanged(@Nullable final List<metcast> metcasts) {
                    for (int i=0; i<metcasts.size(); i++) {
                        mMetcastAdapter.addItem(metcasts.get(i));

                        Call<metcast> Metcast = NetworkService.getInstance().getAPImetcast().getMetcast(
                                metcasts.get(i).getCity(), "token");                      
                        Metcast.enqueue(new Callback<metcast>() {
                            @Override
                            public void onResponse(Call<metcast> call, Response<metcast> response) {
                                if (response.isSuccessful()) {
                                    metcastListViewModel.updateByCity(response.body()); //Из-за этой строки вновь вызывается метод observe
                                    mMetcastAdapter.addItem(response.body());
                                }                     

                            @Override
                            public void onFailure(Call<metcast> call, Throwable t) {
                                mMetcastAdapter.addItems(metcasts);
                                Log.d("1337", "failure " + t);
                            }
                        });
                    }
                }

It is possible not to insert data update into the observe method , but then I don’t know how to get the data and insert it into retrofit for updating

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
visapps, 2019-06-22
@Shadrindmitry

Most likely you have misunderstood how LiveData works. LiveData from Room will always receive updated data when it changes. Standard pattern: in this case, after receiving data from Retrofit, you just need to insert it into Room (through the insert function in Dao with a primary key and OnConflictStrategy.Replace so that old data with the same key is replaced with new ones) and Room will automatically notify listeners via LiveData about changes in this table. Those. just move the request execution logic in Retrofit to a separate method in the ViewModel, such as refreshData(). When you start the activity, immediately call refreshData(), then subscribe to LiveData. In the onChanged method, you only send new data to the adapter and that's it. Thus, onChanged will immediately receive cached data from the database (if it is there), and then when the Retrofit request is executed and the data is inserted into the database, onChanged will be called again and there will be updated data. At the same time, it will be possible to call refreshData () also if the user wants to force a refresh of the data. In onChanged, you now have shitcode written, if you follow the modern architecture of Android applications (using LiveData and ViewModel), a request there with the help of Retrofit is usually done there from the repository, at least from the ViewModel, but not from the activity.

T
tiroman, 2019-06-21
@tiroman

I may offer a banal, but if the data needs to be updated, for example, once at start, then make a boolean variable and change its state after the first update so that it does not run the second time

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question