Keerthi Prasad B V
Keerthi Prasad B V

Reputation: 108

How to use RxJava instead of LiveData with android architecture components?

Note: If you are already using a library like RxJava or Agera, you can continue using them instead of LiveData. But when you use them or other approaches, make sure you are handling the lifecycle properly such that your data streams pause when the related LifecycleOwner is stopped and the streams are destroyed when the LifecycleOwner is destroyed. You can also add the android.arch.lifecycle:reactivestreams artifact to use LiveData with another reactive streams library (for example, RxJava2).

The above statement is copied from android developer page.Here it specifies if you are using RxJava you no need to use LiveData.As both follows Observable Patters.I wanted to know how to Use RxJava instead of LiveData with rest api call.I tried a lot but couldn't find any answer.if someone helps me in solving this issue it will be greatfull. thanks in advance

Upvotes: 2

Views: 3821

Answers (3)

Ghimire
Ghimire

Reputation: 958

This is the Simple Example on which the GET Respnse is parsed using the retrofit with RX

Suppose the response

   {
            "id": "0001",
                "type": "donut",
                "name": "Cake",
                "image": {
            "url": "images/0001.jpg",
                    "width": 200,
                    "height": 200
        },
            "thumbnail": {
            "url": "images/thumbnails/0001.jpg",
                    "width": 32,
                    "height": 32
        }

ApiInterface

 @GET("nestedjson5")
    Observable<NestedJson5Main> nestedJson5();

Image

public class Image {

    String url;
    int width;
    int height;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

Model Class

public class NestedJson5Main {

    String id;
    String type;
    String name;
    Image image;
    Image thumbnail;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public Image getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(Image thumbnail) {
        this.thumbnail = thumbnail;
    }
}

ViewMOdel Class

public class ParsingVm extends BaseObservable {

    Context context;

    ApiInterface mApiInterface;

    public ParsingVm(Context context) {

        this.context = context;
        mApiInterface = AppController.getInstance().getmNetComponent().getApiInterface();

    }

    public Observable<NestedJson5Main> nestedJson5Main() {

        return AppController.getInstance().getmNetComponent().getApiInterface()
                .nestedJson5()
                .subscribeOn(Schedulers.newThread());
    }
    }

ParsingMain

public class ParsingMain extends AppCompatActivity {


    ActivityParsingBinding activityParsingBinding;

    ParsingVm parsingVm;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        activityParsingBinding = DataBindingUtil.setContentView(this, R.layout.activity_parsing);

        parsingVm = new ParsingVm(this);

        activityParsingBinding.setParsingVm(parsingVm);

     receivenesteddjson5();

  }

  public void receivenesteddjson5() {



    parsingVm.nestedJson5Main()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(nestedJson5Main -> {

                Log.d(TAG, "receivenesteddjson5: " + nestedJson5Main.getImage().getHeight());
                Log.d(TAG, "receivenesteddjson5: " + nestedJson5Main.getThumbnail().getUrl());

            }, Throwable -> {

                Throwable.printStackTrace();
            });
}


        }

I have Added Simple Answer on Which the Observable is created and then Observed .

Upvotes: 1

Santanu Sur
Santanu Sur

Reputation: 11477

You can use these methods for conversion of Publisher<T> to LiveData<T> and vice-versa.

  • LiveDataReactiveStreams.fromPublisher(Publisher<T> publisher) -- will give you LiveData object

  • LiveDataReactiveStreams.toPublisher(LifecycleOwner lifecycle, LiveData<T> liveData) --will give your RxJava Publisher<T> object..

Then you can use pub-sub pattern and just subscribe to the publisher .

Upvotes: 2

Tuby
Tuby

Reputation: 3253

Look at

https://developer.android.com/reference/android/arch/lifecycle/LiveDataReactiveStreams

RxJava's Flowable implements Publisher interface. You can convert LiveData to Flowable and go from there.

Upvotes: 2

Related Questions