How can i use socket.io and rxjava 2 android

I am developing an application with mpv (mosby3) + socket.io. I want to use rxjava 2 to communicate with provider and store.

I have a CategoryManager

public class CategoryManager {
    private List<Category> list = null;
    ...
}

      

If the list is not null I can do this

public Single<List<Category>> getList() {
    return Single.just(this.list);
}

      

But if i need to load the list i have async like it

socket.on("category", (data) -> {
    Type founderListType = new TypeToken<ArrayList<Category>>() {}.getType();
    list = gson.fromJson(data[0].toString(), founderListType);
    // here i need to generate event to single subscriber
})

      

I think I should use

public Single<List<Category>> getList(int count) {
    return Single.create(s -> {
        if (list == null) {
           socket.emit("category");
           // i need async load list
        } else {
           s.onSuccess(list.subList(0, count));
        }
    });
}

      

And CategoryPresenter must have a code like

    disposable.add(session.getCategoryManager()
            .getList(5)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(data -> {
                if (isViewAttached()) {
                    getView().setData(data);
                }
            }, throwable -> {
                throwable.printStackTrace();
            })
    );
}

      

I think I can store subscribers in a property of the class

private List<SingleEmmiter> subscribers;

      

and remove the subscribers in the setCancellable method, but I don't think that's a good idea.

Help me please:)

+3


source to share





All Articles