Java 在 onNext() 中不传递任何内容的 Observable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26145917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:54:08  来源:igfitidea点击:

Observable which does not pass anything in onNext()

javarx-java

提问by Oliver Hausler

I would need an Observable, for example to provide a system clock, which does not need to pass anything in onNext(). I couldn't find a signature that would allow me to do that.

我需要一个 Observable,例如提供一个系统时钟,它不需要在 onNext() 中传递任何东西。我找不到允许我这样做的签名。

Sure, I could use any object and then pass null, but that doesn't make much sense. So my question is if there is a better way to do that.

当然,我可以使用任何对象然后传递 null,但这没有多大意义。所以我的问题是是否有更好的方法来做到这一点。

Observable.create(new Observable.OnSubscribe<Anyobject>() { // use any object in the signature

            @Override public void call(Subscriber<? super Anyobject> subscriber) {

                    subscriber.onNext(null); // then pass null
                    subscriber.onCompleted();

            }

        })

采纳答案by Miguel Lavigne

You don't need to call onNextif your Observabledoesn't emit anything. You could use Voidin your signature and do something like

onNext如果你Observable不发出任何东西,你就不需要打电话。您可以Void在您的签名中使用并执行类似的操作

Observable<Void> o = Observable.create(new Observable.OnSubscribe<Void>() {
    @Override
    public void call(Subscriber<? super Void> subscriber) {
        // Do the work and call onCompleted when you done,
        // no need to call onNext if you have nothing to emit
        subscriber.onCompleted();
    }
});

o.subscribe(new OnCompletedObserver<Void>() {
    @Override
    public void onCompleted() {
        System.out.println("onCompleted");
    }

    @Override
    public void onError(Throwable e) {
        System.out.println("onError " + e.getMessage());
    }
});

You can define an OnCompletedObserver to simplify your Observer callback so that you don't have to override the onNext since you don't need it.

你可以定义一个 OnCompletedObserver 来简化你的观察者回调,这样你就不必覆盖 onNext 因为你不需要它。

public abstract class OnCompletedObserver<T> implements Observer<T> {
    @Override
    public void onNext(T o) {

    }
}

If I've understood what you're asking then this should do the trick.

如果我明白你在问什么,那么这应该可以解决问题。

回答by Mike Strobel

If you need something to be passed to onNext()before onCompleted()is called:

如果您需要将某些东西传递给onNext()before ,onCompleted()则调用:

Observable.<Void>just(null)

If you only need onCompleted()to be called:

如果您只需要onCompleted()被调用:

Observable.empty()

回答by Johnny

I don't know this will helps you or not.
The code written in RxSwift.

我不知道这对你有没有帮助。
中编写的代码RxSwift

// Create an observable and emit somethings
let myObservable = Observable<Void>.create{ observer in
    observer.on(.next(Void()))
    return Disposables.create()
}

// Observer subscribe changes
myObservable.subscribe(onNext: {
    _ in
    print("Hello")
}).disposed(by: disposeBag)

Or use the Variable object

或者使用 Variable 对象

// Create a Variable object that contanins nothing
var myValueWatcher:Variable<Void> = Variable<Void>(Void())

// Observer subscribe changes
myValueWatcher.asObservable().skip(1).subscribe(onNext: {
    _ in
    print("Changes!")
}).disposed(by: disposeBag)


// The emit code
myValueWatcher.value = Void()

回答by mixel

RxJava 2 Wiki:

RxJava 2 维基

RxJava 2.x no longer accepts null values and the following will yield NullPointerException immediately or as a signal to downstream.

...

This means that Observable<Void>can no longer emit any values but only terminate normally or with an exception. API designers may instead choose to define Observable<Object>with no guarantee on what Object will be (which should be irrelevant anyway)

RxJava 2.x 不再接受空值,以下将立即产生 NullPointerException 或作为下游的信号。

...

这意味着Observable<Void>不能再发出任何值,而只能正常终止或异常终止。API 设计者可能会选择在Observable<Object>不保证对象将是什么的情况下进行定义(无论如何这应该无关紧要)

It means that you can't use Voidand do Observable.just(null).

这意味着你不能使用Void和做Observable.just(null).

Use Objector some other simple type instead:

改用Object或其他一些简单类型:

Observable.just(new Object());

回答by Leo Droidcoder

One of the light solutions is to use Observable<Boolean>

轻量级解决方案之一是使用 Observable<Boolean>

And then onNext(Boolean.TRUE)which you then just ignore.

然后onNext(Boolean.TRUE)你就忽略它。

But probably you shouldn't use Observable in that case.
Consider using Completableinstead

但在这种情况下,您可能不应该使用 Observable。
考虑使用Completable代替

回答by AllDayAmazing

Starting with RxJava 2, the propper way to do this is to use a Completable

从 RxJava 2 开始,正确的方法是使用 Completable

From the docs:

从文档

Represents a deferred computation without any value but only indication for completion or exception. The class follows a similar event pattern as Reactive-Streams: onSubscribe (onError|onComplete)?

表示没有任何值但仅指示完成或异常的延迟计算。该类遵循与 Reactive-Streams 类似的事件模式: onSubscribe (onError|onComplete)?