Not updating ViewModel after Retrofit CREATE request

39
January 13, 2019, at 01:20 AM

I'm using the Retrofit client to perform HTTP requests in a simple Android project. When I send a CREATE request, the item is successfully created in the remote SQLite database, but the ViewModel still contains the old list. When I restart my application, and fetch again all items from the server, the item appears.

I'm following the Android - Guide to app archcitecture and I use LiveData objects both in my Repository and ViewModel.

My Fragment adds an Observer to the LiveData object in ViewModel:

mViewModel.getAllItems().observe(getActivity(), new Observer<List<Item>>() {
            @Override
            public void onChanged(@Nullable final List<Item> items) {
                // Update the cached copy of the words in the adapter.
                mAdapter.setItems(items);
                mAdapter.notifyDataSetChanged();
            }
        });

The onChange() function is fired after I send a CREATE request, but the list of items has not changed.

Retrofit version:

// Retrofit
implementation('com.squareup.retrofit2:retrofit:2.5.0') {
    exclude module: 'okhttp'
}
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
implementation 'com.squareup.okhttp3:okhttp:3.6.0'
implementation "com.squareup.okhttp3:logging-interceptor:3.6.0"

EDIT

Relevant code:

ItemViewModel.java

public class ItemViewModel extends AndroidViewModel {
    private ItemRepository mItemRepository;
    private LiveData<List<Item>> items;
    public ItemViewModel(Application application) {
        super(application);
        this.mItemRepository = new ItemRepository();
    }
    public LiveData<List<Item>> getAllItems() {
        if (this.items == null) {
            this.items = mItemRepository.getAllItems();
        }
        return this.items;
    }
    public void createItem(Item item) {
        this.mItemRepository.createItem(item);
    }
}

ItemRepository.java

public class ItemRepository {
    private WebService mWebservice;
    private MutableLiveData<List<Item>> mAllItems = new MutableLiveData<>();
    public ItemRepository() {
        this.mWebservice = WebClient.getClient().create(WebService.class);
    }
    public LiveData<List<Item>> getAllItems() {
        final MutableLiveData<List<Item>> data = new MutableLiveData<>();
        this.mWebservice.getAllItems().enqueue(new Callback<GetAllItemsResponse>() {
            @Override
            public void onResponse(Call<GetAllItemsResponse> call, Response<GetAllItemsResponse> response) {
                data.setValue(response.body().items);
            }
            @Override
            public void onFailure(Call<GetAllItemsResponse> call, Throwable t) {
                System.out.println(t);
            }
        });
        return data;
    }
    public void insertItem(Item item) {
        mWebservice.createItem(item);
    }
}
Answer 1
public class ItemViewModel extends AndroidViewModel {
    private ItemRepository mItemRepository;
    private LiveData<List<Item>> items;
    public ItemViewModel(Application application) {
        super(application);
        this.mItemRepository = new ItemRepository();
    }
    public LiveData<List<Item>> getAllItems() {
            this.items = mItemRepository.getAllItems();
        return this.items;
    }
    public void createItem(Item item) {
        this.mItemRepository.createItem(item);
    }
}

try this remove if condition

READ ALSO
how to execute a service correctly with @POST in java with retrofit2

how to execute a service correctly with @POST in java with retrofit2

I'm using the retrofit2 library, the method I use is an @POST but I do not understand why it gives me an error, I currently send the data in an @Body however in postman I have it in the following way

52
apk signed with a different certificate problem

apk signed with a different certificate problem

My original application was created in Android Studio and I have updated it by appybuilder

54
Retrieve preferences from intelliJ platform IDE

Retrieve preferences from intelliJ platform IDE

I am writing plugin for Android Studio in Java and currently need to determine user's android SDK locationAs far as I know, the location of SDK can be set by user in: Android Studio -> Preferences -> Appearance and Behavior -> System Settings -> Android...

53
Remove &ldquo;view more&rdquo; button in WhatsApp stickers?

Remove “view more” button in WhatsApp stickers?

There are some sticker packs for WhatsApp that don't show a "view more" button on their click menuThere is a line in a smali file which defines to which Play Store app the button should take

33