Mahmoud Essam
Mahmoud Essam

Reputation: 23

RXjava with MVVM and Retrofit

I want to use RxJava with MVVM but I don't understand why it doesn't respond CompositeDisposable with my Response class

I want to deal with RxJava that is by but I don't know how to link CompositeDisposable with

LiveData

...

LiveData
     public
        LiveData <HomeResponse> setMenu() {
            final MutableLiveData <HomeResponse> data = new MutableLiveData <>( );

                compositeDisposable.add(apiRequest.getMenu()
                                                .subscribeOn( Schedulers.io())
                                                .observeOn( AndroidSchedulers.mainThread())
                                                .subscribe( new Consumer <Response<HomeResponse>>() {
                                                    @Override
                                                    public void accept(Response<HomeResponse>
                                                     registrationModelResponse) throws Exception  {

                                                       data.setValue(registrationModelResponse.body());
                                                    }
                                                }));



            return data;
        }

..... Response class :

  public
class HomeResponse {

    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("totalResults")
    @Expose
    private Integer totalResults;
    @SerializedName("Menu")
    @Expose
    private List <MainMenu> MainMenu = null;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Integer getTotalResults() {
        return totalResults;
    }

    public void setTotalResults(Integer totalResults) {
        this.totalResults = totalResults;
    }

    public List<MainMenu> getMainMenu() {
        return MainMenu;
    }

    public void setAUsers(List<MainMenu> MainMenu) {
        this.MainMenu = MainMenu;
    }
}

..... my API :

  @GET("getmenu.php")
   Observable<List <HomeResponse>> getMenu();

.............. error hear ...

 .subscribe( new Consumer <Response<HomeResponse>>() {
                                                @Override
                                                public void accept(Response<HomeResponse>
                                                 registrationModelResponse) throws Exception  {

                                                   data.setValue(registrationModelResponse.body());
                                                }
                                            }));

enter image description here

Upvotes: 1

Views: 1439

Answers (1)

Hello World
Hello World

Reputation: 740

I think's maybe you confuse how to handle LiveData.

Regularly, if you want to get value from LiveData, your activity or fragment is the one who need to observe it. Like this example.

// I have 'valueA' as variable in viewModel.
val valueA = MutableLiveData<String>()


// In Main Activity. I observe 'valueA' here
fun initObserver() {
    viewModel.valueA.observe(this, Observer { it -> // it is value of livedata 'data'
        // do something with 'valueA'.
    })
}

And your setMenu() function should not return value from API because when you call API like that it's asynchronous call. So when you return your value your API call still not complete.

This should be your function.

public void setMenu() {
        final MutableLiveData <List<HomeResponse>> data = new MutableLiveData <>( );

            compositeDisposable.add(apiRequest.getMenu()
                                            .subscribeOn( Schedulers.io())
                                            .observeOn( AndroidSchedulers.mainThread())
                                            .subscribe( new Consumer <List<HomeResponse>>() {
                                                @Override
                                                public void accept(List<HomeResponse>
                                                 registrationModelResponse) throws Exception  {

                                                   data.setValue(registrationModelResponse);
                                                }
                                            }));



    }

PS. Sorry for my terrible English skill.

PS2. Your Response from API will return List<HomeResponse>, but in your comsumer it's become Result<HomeResponse> so it's will cause error.

Upvotes: 1

Related Questions