本文整理汇总了Java中io.reactivex.observables.ConnectableObservable类的典型用法代码示例。如果您正苦于以下问题:Java ConnectableObservable类的具体用法?Java ConnectableObservable怎么用?Java ConnectableObservable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConnectableObservable类属于io.reactivex.observables包,在下文中一共展示了ConnectableObservable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doSomeWork
import io.reactivex.observables.ConnectableObservable; //导入依赖的package包/类
private void doSomeWork() {
PublishSubject<Integer> source = PublishSubject.create();
ConnectableObservable<Integer> connectableObservable = source.replay(3); // bufferSize = 3 to retain 3 values to replay
connectableObservable.connect(); // connecting the connectableObservable
connectableObservable.subscribe(getFirstObserver());
source.onNext(1);
source.onNext(2);
source.onNext(3);
source.onNext(4);
source.onComplete();
/*
* it will emit 2, 3, 4 as (count = 3), retains the 3 values for replay
*/
connectableObservable.subscribe(getSecondObserver());
}
示例2: doSomeWork
import io.reactivex.observables.ConnectableObservable; //导入依赖的package包/类
private void doSomeWork() {
Observable<Long> observable = Observable.interval(1, TimeUnit.SECONDS).take(6);
//使用publish操作符将普通Observable转换为可连接的Observable
ConnectableObservable<Long> connectableObservable = observable
// .publish();
.replay(5);
//第一个订阅者订阅,不会开始发射数据
connectableObservable
.compose(Utils.<Long>ioToMain())
.subscribe(getFirstObserver());
//如果不调用connect方法,connectableObservable则不会发射数据
//即使没有任何订阅者订阅它,你也可以使用connect方法让一个Observable开始发射数据(或者开始生成待发射的数据)。
//这样,你可以将一个”冷”的Observable变为”热”的。
connectableObservable.connect();
//第二个订阅者延迟2s订阅,这将导致丢失前面2s内发射的数据
connectableObservable.delaySubscription(2, TimeUnit.SECONDS)//0,1数据丢失
.compose(Utils.<Long>ioToMain())
.subscribe(getSecondObserver());
}
示例3: postTextOnlyTweet
import io.reactivex.observables.ConnectableObservable; //导入依赖的package包/类
private void postTextOnlyTweet(String status) {
mProgressDialog.show();
ConnectableObservable<StatusUpdate> observable =
mTwitterApi.postTweet(status, null, mLatitude, mLongitude)
.subscribeOn(Schedulers.io())
.publish();
Disposable postingDisposable = observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::onSuccessfulTweetPosting, this::onErrorTweetPosting);
mCompositeDisposable.add(postingDisposable);
Disposable crossPostingDisposable = observable
.flatMap(this::pushTweetToLoklak)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
push -> Log.e(LOG_TAG, push.getStatus()),
t -> Log.e(LOG_TAG, "Cross posting failed: " + t.toString())
);
mCompositeDisposable.add(crossPostingDisposable);
Disposable publishDisposable = observable.connect();
mCompositeDisposable.add(publishDisposable);
}
示例4: main
import io.reactivex.observables.ConnectableObservable; //导入依赖的package包/类
public static void main(String[] args) {
ConnectableObservable<Integer> threeRandoms =
Observable.range(1, 3)
.map(i -> randomInt()).publish();
//Observer 1 - print each random integer
threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i));
//Observer 2 - sum the random integers, then print
threeRandoms.reduce(0, (total, next) -> total + next)
.subscribe(i -> System.out.println("Observer 2: " + i));
threeRandoms.connect();
}
示例5: main
import io.reactivex.observables.ConnectableObservable; //导入依赖的package包/类
public static void main(String[] args) {
ConnectableObservable<Integer> threeRandoms =
Observable.range(1, 3)
.map(i -> randomInt()).publish();
threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i));
threeRandoms.subscribe(i -> System.out.println("Observer 2: " + i));
threeRandoms.connect();
}
示例6: main
import io.reactivex.observables.ConnectableObservable; //导入依赖的package包/类
public static void main(String[] args) {
ConnectableObservable<Integer> threeIntegers =
Observable.range(1, 3).publish();
threeIntegers.subscribe(i -> System.out.println("Observer One:" + i));
threeIntegers.subscribe(i -> System.out.println("Observer Two:" + i));
threeIntegers.connect();
}
示例7: main
import io.reactivex.observables.ConnectableObservable; //导入依赖的package包/类
public static void main(String[] args) {
ConnectableObservable<Integer> threeRandoms =
Observable.range(1, 3)
.map(i -> randomInt()).publish();
threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i));
threeRandoms.subscribe(i -> System.out.println("Observer 2: " + i));
threeRandoms.connect();
}
示例8: main
import io.reactivex.observables.ConnectableObservable; //导入依赖的package包/类
public static void main(String[] args) {
ConnectableObservable<Integer> threeInts =
Observable.range(1, 3).publish();
Observable<Integer> threeRandoms = threeInts.map(i ->
randomInt());
threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i));
threeRandoms.subscribe(i -> System.out.println("Observer 2: " + i));
threeInts.connect();
}
示例9: main
import io.reactivex.observables.ConnectableObservable; //导入依赖的package包/类
public static void main(String[] args) {
ConnectableObservable<Integer> threeRandoms =
Observable.range(1, 3)
.map(i -> randomInt()).publish();
//Observer 1 - print each random integer
threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i));
//Observer 2 - sum the random integers, then print
threeRandoms.reduce(0, (total, next) -> total + next)
.subscribe(i -> System.out.println("Observer 2: " + i));
threeRandoms.connect();
}
示例10: main
import io.reactivex.observables.ConnectableObservable; //导入依赖的package包/类
public static void main(String[] args) {
ConnectableObservable<Long> seconds =
Observable.interval(1, TimeUnit.SECONDS).publish();
//observer 1
seconds.subscribe(l -> System.out.println("Observer 1: " + l));
seconds.connect();
//sleep 5 seconds
sleep(5000);
//observer 2
seconds.subscribe(l -> System.out.println("Observer 2: " + l));
//sleep 5 seconds
sleep(5000);
}
示例11: main
import io.reactivex.observables.ConnectableObservable; //导入依赖的package包/类
public static void main(String[] args) {
ConnectableObservable<String> source =
Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon")
.publish();
//Set up observer 1
source.subscribe(s -> System.out.println("Observer 1: " + s));
//Set up observer 2
source.map(String::length)
.subscribe(i -> System.out.println("Observer 2: " + i));
//Fire!
source.connect();
}
示例12: freeFlowEmps
import io.reactivex.observables.ConnectableObservable; //导入依赖的package包/类
@Override
public ConnectableObservable<String> freeFlowEmps() {
List<String> rosterNames = new ArrayList<>();
Function<Employee, String> familyNames = (emp) -> emp.getLastName().toUpperCase();
ConnectableObservable<String> flowyNames = Observable.fromIterable(employeeDaoImpl.getEmployees()).map(familyNames).cache().publish();
flowyNames.subscribe(System.out::println);
flowyNames.subscribe((name) ->{
rosterNames.add(name);
});
System.out.println(rosterNames);
return flowyNames;
}
示例13: execute
import io.reactivex.observables.ConnectableObservable; //导入依赖的package包/类
/**
* If the receiver is enabled, this method will:
* <p>
* 1. Invoke the `func` given at the time of creation.
* 2. Multicast the returned observable.
* 3. Send the multicasted observable on {@link #executionObservables()}.
* 4. Subscribe (connect) to the original observable on the main thread.
*
* @param input The input value to pass to the receiver's `func`. This may be null.
* @return the multicasted observable, after subscription. If the receiver is not
* enabled, returns a observable that will send an error.
*/
@MainThread
public final Observable<T> execute(@Nullable Object input) {
boolean enabled = mImmediateEnabled.blockingFirst();
if (!enabled) {
return Observable.error(new IllegalStateException("The command is disabled and cannot be executed"));
}
try {
Observable<T> observable = mFunc.apply(input);
if (observable == null) {
throw new RuntimeException(String.format("null Observable returned from observable func for value %s", input));
}
// This means that `executing` and `enabled` will send updated values before
// the observable actually starts performing work.
final ConnectableObservable<T> connection = observable
.subscribeOn(AndroidSchedulers.mainThread())
.replay();
mAddedExecutionObservableSubject.onNext(connection);
connection.connect();
return connection;
} catch (Exception e) {
e.printStackTrace();
return Observable.error(e);
}
}
示例14: testRefCount
import io.reactivex.observables.ConnectableObservable; //导入依赖的package包/类
private void testRefCount() {
Observable<Long> observable = Observable.interval(1, TimeUnit.SECONDS).take(6);
ConnectableObservable<Long> connectableObservable = observable.publish();
connectableObservable.connect();
Observable<Long> longObservable = connectableObservable.refCount();
longObservable.delaySubscription(2, TimeUnit.SECONDS)
.compose(Utils.<Long>ioToMain())
.subscribe(getFirstObserver());
longObservable.compose(Utils.<Long>ioToMain()).subscribe(getSecondObserver());
}
示例15: checkSyncStartAndFinishEvents
import io.reactivex.observables.ConnectableObservable; //导入依赖的package包/类
private void checkSyncStartAndFinishEvents(ConnectableObservable<BaseEvent> syncEventBus) {
syncEventBus
.test()
.awaitCount(2, () -> {
}, MAX_TIMEOUT_MILLIS)
.assertNoErrors()
.assertValueAt(0, event -> {
assertThat(event).isInstanceOf(SyncStartEvent.class);
return true;
})
.assertValueAt(1, event -> {
assertThat(event).isInstanceOf(SyncFinishEvent.class);
return true;
});
}