Skip to content

xiaoman-team/RxAndroidLive

Repository files navigation

RxAndroidLive

License Jcenter

An rxjava implementation of Livedata for Lifecycle cache and use Active or unActive

Installation

Using JCenter:

repositories {
    jcenter()
}

dependencies {
    compile 'cn.xiaoman.library.android:rxandroidlive:1.0.0'
}

Requirements

RxJava 2.1.0+ AndroidX Lifecycle 2.1.0+

Usage

Just enable RxService as soon as possible. In Android, for example:

class MainActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Flowable.interval(1, TimeUnit.SECONDS)
                .compose(RxAndroidLive.<Long>bindLifecycle(this, true, AndroidSchedulers.mainThread()))
                .subscribe(new Subscriber<Long>() {
                    @Override
                    public void onSubscribe(Subscription s) {
                        addContent("Flowable onSubscribe");
                        final int requestNum = 10;
                        s.request(requestNum);
                        addContent("Flowable request " + requestNum);
                    }

                    @Override
                    public void onNext(Long aLong) {
                        addContent("Flowable onNext value " + aLong);

                    }

                    @Override
                    public void onError(Throwable t) {
                        addContent("Flowable onError");

                    }

                    @Override
                    public void onComplete() {
                        addContent("Flowable onComplete");
                    }
                });
    }
}

Screenshot

avatar

avatar