Ad

How To Handle Exception In Mvvm Android And Pass That Exception To Our View

I am using MVVM architecture to hit a web service through retrofit in android studio. I have handled the response of the service in my view class. But the problem i am facing is how to handle the exceptions and pass them to my view class. One way is to make constructor in my Bean class and pass both the response and error to it and update UI. But i want more optimised way to handle exceptions inside UI.

Here is my repository code :

final MutableLiveData<MyBeanClass> myBeanClass = new MutableLiveData<>();ApiInterface apiInterface = ApiClient.getClientAuthentication().create(ApiInterface.class);
Call<MyBeanClass> call = apiInterface.getData(id);
call.enqueue(new Callback<MyBeanClass>() {@Override
    public void onResponse(Call<MyBeanClass> call, Response<MyBeanClass> response) {
        if(response.body()!=null) {
            myBeanClass.setValue(response.body());
        }
    }

    @Override
    public void onFailure(Call<MyBeanClass> call, Throwable t) {
     //How to handle exceptions here and pass the exception to UI without making constructor in bean class
    }
});

return myBeanClass;
Ad

Answer

You could wrap your Bean class in a Generic Resource class and observe it. Google has mentioned it in their docs:

public class Resource<T> {

    @NonNull public final Status status;
    @Nullable public final T data;
    @Nullable public final String message;

    private Resource(@NonNull Status status, @Nullable T data,
            @Nullable String message) {
        this.status = status;
        this.data = data;
        this.message = message;
    }

    public static <T> Resource<T> success(@NonNull T data) {
        return new Resource<>(Status.SUCCESS, data, null);
    }

    public static <T> Resource<T> error(String msg, @Nullable T data) {
        return new Resource<>(Status.ERROR, data, msg);
    }

    public static <T> Resource<T> loading(@Nullable T data) {
        return new Resource<>(Status.LOADING, data, null);
    }

    public enum Status { SUCCESS, ERROR, LOADING }
}

More can be found here:Google Docs

Do like this:

final MutableLiveData<Resource<MyBeanClass>> myBeanClass = new MutableLiveData<>();
ApiInterface apiInterface = 
        ApiClient.getClientAuthentication().create(ApiInterface.class);

Call<Response<MyBeanClass>> call = apiInterface.getData(id);
call.enqueue(new Callback<Resource<MyBeanClass>>() {
    @Override
    public void onResponse(Call<MyBeanClass> call, 
            Response<Resource<MyBeanClass>> response) {
        if (response.body() != null) {
            myBeanClass.setValue(Resource.success(response.body()));
        }
    }

    @Override
    public void onFailure(Call<MyBeanClass> call, Throwable t) {
         myBeanClass.setValue(Resource.error(t.getMessage), null);
    }
});
return myBeanClass;

Now similarly, observe in your ViewModel Bean class wrapped in Resource. Handle success and error according to the Resource classes Status

Ad
source: stackoverflow.com
Ad