Will the `onChanged` method trigger just as we change the data? Even if we dont change the state of activity?
Asked Answered
P

2

0

Will the onChanged method be called if we dont change the state of the activity or restart the activity? ( Even if the data changes ) . If it doesnt happen how shall i make it happen ?

onCreate() {
    viewModel.getHolidaysDatesFromServer(..).observe(.....this,new Observer<GetHolidaysDatesFromServer>) {
        onChanged(GetHolidaysDatesFromServer GetHolidaysDatesFromServer)
    }    
}

and my ViewModel class ...

public class CalendarLifeCycleOwner extends AndroidViewModel {
    Context context;
    MutableLiveData<GetHolidaysDatesFromServer> MutableLiveDataGetHolidays=new MutableLiveData<>();
    public CalendarLifeCycleOwner(@NonNull Application application) {
        super(application);
        this.context=application.getApplicationContext();
    }

    public MutableLiveData<GetHolidaysDatesFromServer> getHolidaysDatesFromServer(String upd_token,int product_id) {
        JustProductId justProductId=new JustProductId();
        justProductId.setProduct_id(product_id);
        UserListInterface userListInterface= UserListService.createService(UserListInterface.class, Utility.ACCEPT_HEADER_V4);
        Call<GetHolidaysDatesFromServer> getHolidaysDatesFromServerCall=userListInterface.GetProductHolidays(upd_token,justProductId);
        getHolidaysDatesFromServerCall.enqueue(new Callback<GetHolidaysDatesFromServer>() {
            @Override
            public void onResponse(Call<GetHolidaysDatesFromServer> call, Response<GetHolidaysDatesFromServer> response) {
                if (response.isSuccessful()) {
                    GetHolidaysDatesFromServer getHolidaysDatesFromServer=response.body();
                    MutableLiveDataGetHolidays.postValue(getHolidaysDatesFromServer);
                }
            }

            @Override
            public void onFailure(Call<GetHolidaysDatesFromServer> call, Throwable t) {
                ErrorObject errorObject=new ErrorObject(t);
                GetHolidaysDatesFromServer getHolidaysDatesFromServer=new GetHolidaysDatesFromServer();
                getHolidaysDatesFromServer.setErrorObject(errorObject);
                MutableLiveDataGetHolidays.setValue(getHolidaysDatesFromServer);

            }
        });
        return MutableLiveDataGetHolidays;
    }
}

But after i just change the data onChanged() is not getting called ...

Precast answered 19/1, 2018 at 12:51 Comment(21)
If you restart your activity than it will be called or when you change the MutableLiveDataFeatured
i have changed the data..but its not getting called unless i restart or rotate or anything ... do i always have to write .observe(....) to make it trigger ..?? i have observed in oCreate oncePrecast
You need to write only once on onCreate...please post some code to be more clearFeatured
Are you using ViewModel with LiveData ??Featured
I am using ViewModel with MutableLiveData ...1st time in onCreate its getting called if the data is changed ...but after that even if the data changes onChanged is not calledPrecast
After i rotate or change the activity state its getting called..it isnt getting called just by changing the data ..Precast
How you are changing the data ? please post some codeFeatured
have added pseudo code...its just simple as that...i am getting the response from retrofit async using enqueue..Precast
i am setting the value of mutablelivedata by mutablelivedata.setValue(DataObject) do i need to use .postValue instead ?Precast
If you are changing the value from a WorkerThread or off the UIThread than you need to use postValueFeatured
same..issue both with both the operator (setValue and postValue)Precast
Need more code to clarify the issueFeatured
Ok i am posting ViewModel class codePrecast
already updated code... just observed once in onCreatePrecast
Did you check is respone is sucessful ? and use setValue on onResponse method it runs on UIThread onlyFeatured
My code is fine i just wanna know just as the data changes does the observer gets triggered or do i need change the activity state?? and Ya i used setValue at first... both giving same result...Precast
You don't need to change the state it will automatically updated the livedataFeatured
then it isnt but it should.. :( do i need to add anything else ?Precast
Your code is fine..just put the debug on onRespone() callback and check that is it respone.isSuccessfull() is true ?...that's the only thing i have to sayFeatured
ya thats true.. if it wasnt true ...if i rotate activity then it would throw error... just that automatically its not updating ..i am having to change the state... ok i am debugging ..Precast
debugged...the onResponse method is called 1st time then i change data... its not called , then i rotate ,then again its called 2nd timePrecast
R
1

Google actually described that (or very familiar to that) kind of issue as an anti-pattern on the dev-summit. The recommended approach is to use switchMap from android.arch.lifecycle.Transformations class. You can see the documentation page for more details.

Raina answered 9/11, 2018 at 9:22 Comment(0)
F
0

Try using this and delete the global MutableLiveData variable

public MutableLiveData<GetHolidaysDatesFromServer> getHolidaysDatesFromServer(String upd_token,int product_id) {

       MutableLiveData<GetHolidaysDatesFromServer> MutableLiveDataGetHolidays=new MutableLiveData<>();

       //Your API call code goes here

       return MutableLiveDataGetHolidays;
 }
Featured answered 19/1, 2018 at 13:49 Comment(7)
Unfortunately same issue...is there any other method for retrofit callbacks?? like retrofit+viewmodel?Precast
Just try changing the livedata value without retrofit and remove retrofit callback instead to test setValue after some duration to find the issue where it liesFeatured
then i also have to call the request after specific duration... like for(infinite){ call.enque...} I think this will work.. but continously i have to keep calling APIPrecast
I didn't get you @SantanuSurFeatured
and it isnt working too.. :p see now i got the reason why it isnt updating but i dont know how to solve it... its not updating because 1st time as the response is received the onChanged method is called... 2nd time i update the data to server... but i am not calling the getHolidaysDatesFromServer 2nd time in the UIPrecast
did u get my point ?? i have to manually call the getHolidayDatesFromServer after i update... but this doesnt make sense ..why shall i have to call it again after i update... (i am updating to server ) ..Precast
Checkout my example here github.com/burhanrashid52/Android-Room-Data-Persistent and compare it your codeFeatured

© 2022 - 2024 — McMap. All rights reserved.