标签: rx-java

RxJava2 阻塞可观察超时:超时触发时如何发出结果?

我有一个 Observable,它以 JSON 字符串的形式发出事件。我想订阅它阻塞并获取第一个字符串。不是问题。

但我也想超时并在超时触发时发出一个罐装 JSON 字符串。

这是我现有的代码:

String jsonString = rxEvents.subscribe().toBlocking().first();
Run Code Online (Sandbox Code Playgroud)

blocking observable rx-java rx-java2

5
推荐指数
1
解决办法
3741
查看次数

RxJava2CallAdapterFactory.create() 在改造中的作用是什么

RxJava2CallAdapterFactory.create() 的作用是什么...

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.example.com")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
Run Code Online (Sandbox Code Playgroud)

android rx-java

5
推荐指数
1
解决办法
2622
查看次数

RxJava:结合两个可选的 observable

我有两个Observables,我们称它们为PeanutButter和Jelly。我想将它们组合到一个Sandwich Observable. 我可以使用:

Observable<PeanutButter> peanutButterObservable = ...;
Observable<Jelly> jellyObservable = ...;
Observable<Sandwich> sandwichObservable = Observable.combineLatest(
    peanutButterObservable,
    jellyObservable,
    (pb, j) -> makeSandwich(pb, j))
Run Code Online (Sandbox Code Playgroud)

问题是 RX在发射第一个组合之前等待第一个PeanutButter和第一个Jelly发射,Sandwich但Jelly可能永远不会发射,这意味着我永远不会得到第一个Sandwich。

我想组合这两个提要,以便在发出任一提要的第一个项目时立即发出组合项目,无论另一个提要是否尚未发出任何内容,我该如何在 RxJava 中执行此操作?

java reactive-programming rx-java reactive rx-java2

5
推荐指数
1
解决办法
2524
查看次数

如何在 onComplete 之后显式取消订阅 observable

在下面的代码中,在完成?之后,如何以及在哪里放置unSubscribe以Observable明确取消订阅onComplete。

getObservable()
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.io())
    .subscribe(new Subscriber<Boolean>() {
        @Override
        public void onCompleted() {
            doSomething();
        }

        @Override
        public void onError(Throwable e) {
            thereIsError();
        }

        @Override
        public void onNext(Boolean status) {
            updateView();
        }
    });
Run Code Online (Sandbox Code Playgroud)

rx-java rx-android rx-java2

5
推荐指数
2
解决办法
7326
查看次数

默认情况下,运算符不对任何特定的调度程序进行操作是什么意思

默认情况下,运算符不对任何特定的调度程序进行操作是什么意思?

示例:CombineLatest。

RxJava 将这个操作符实现为 combineLatest。它可能需要 2 到 9 个 Observables(以及组合函数)作为参数,或者单个 Observables 列表(以及组合函数)。默认情况下,它不会对任何特定的调度程序进行操作。

rx-java

5
推荐指数
1
解决办法
356
查看次数

RxJava Zip Observable Iterables

我想压缩一个Observable<List<Int>>.

fun testObservablezip() {
    val jobs = mutableListOf<Observable<List<Int>>>()
    for (i in 0 until 100 step 10) {
        val job = Observable.fromArray(listOf(i + 1, i + 2, i + 3))
        jobs.add(job)
    }

    val listMerger = Function<Array<List<Int>>, List<Int>> { it.flatMap { it } }
    Observable.zip(jobs, listMerger) // No valid function parameters
}
Run Code Online (Sandbox Code Playgroud)

即使listMerger定义了它的输入和输出,zip也不接受它。

observable kotlin rx-java rx-java2

5
推荐指数
1
解决办法
2514
查看次数

OkHttp 上的不平衡进入/退出

所以我经常有一个例外说java.lang.IllegalStateException: Unbalanced enter/exit,这发生在onStop我的活动调用disposables.clear().

我之前问过它并被告知这是一个 D8 错误,但我现在使用的是最新的 D8,或者任何最新的 Android Studio 3.1.3 并且它仍在发生。

我还在github 上为 OkHttp 询问了它,并被告知要找到一种调用call.cancel().

所以我的问题是,我怎样才能disposables.clear()打电话call.cancel()?

这是例外:

Caused by java.lang.IllegalStateException: Unbalanced enter/exit
    at okio.AsyncTimeout.enter(AsyncTimeout.java:73)
    at okio.AsyncTimeout$2.read(AsyncTimeout.java:235)
    at okio.RealBufferedSource.read(RealBufferedSource.java:47)
    at okhttp3.internal.http1.Http1Codec$AbstractSource.read(Http1Codec.java:363)
    at okhttp3.internal.http1.Http1Codec$ChunkedSource.read(Http1Codec.java:453)
    at okio.RealBufferedSource.read(RealBufferedSource.java:47)
    at okhttp3.internal.cache.CacheInterceptor$1.read(CacheInterceptor.java:174)
    at okhttp3.internal.Util.skipAll(Util.java:175)
    at okhttp3.internal.Util.discard(Util.java:157)
    at okhttp3.internal.cache.CacheInterceptor$1.close(CacheInterceptor.java:202)
    at okio.RealBufferedSource.close(RealBufferedSource.java:469)
    at okio.RealBufferedSource$1.close(RealBufferedSource.java:453)
    at java.io.BufferedInputStream.close(BufferedInputStream.java:472)
    at java.nio.channels.Channels$ReadableByteChannelImpl.implCloseChannel(Channels.java:255)
    at java.nio.channels.spi.AbstractInterruptibleChannel$1.interrupt(AbstractInterruptibleChannel.java:166)
    at java.lang.Thread.interrupt(Thread.java:956)
    at java.util.concurrent.FutureTask.cancel(FutureTask.java:146)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.cancel(ScheduledThreadPoolExecutor.java:258)
    at io.reactivex.internal.schedulers.ScheduledRunnable.dispose(ScheduledRunnable.java:107)
    at io.reactivex.disposables.CompositeDisposable.dispose(CompositeDisposable.java:217)
    at io.reactivex.disposables.CompositeDisposable.dispose(CompositeDisposable.java:80) …
Run Code Online (Sandbox Code Playgroud)

android rx-java okhttp rx-android okhttp3

5
推荐指数
1
解决办法
1435
查看次数

无法解析 io.reactivex.rxjava2:rxjava:2.2.0

我以前从未使用过这个库,现在我正在尝试使用它并添加所需的依赖项,但项目没有被同步。这是我的构建 gradle :

dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])

        // implement support libraries
        implementation 'com.android.support:appcompat-v7:27.0.2'
        implementation 'com.android.support:animated-vector-drawable:27.0.2'
        implementation 'com.android.support:design:27.0.2'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        implementation 'com.android.support:support-v4:27.0.2'

        // implement firebase libraries
        implementation 'com.google.firebase:firebase-auth:11.0.4'
        implementation 'com.google.firebase:firebase-database:11.0.4'
        implementation 'com.google.firebase:firebase-storage:11.0.4'
        implementation 'com.google.firebase:firebase-core:11.0.4'

        // implement firebase-ui
        implementation 'com.firebaseui:firebase-ui-database:2.3.0'

        // implement lifecycle
        implementation 'android.arch.lifecycle:extensions:1.1.0'

        // implement circle image view
        implementation 'de.hdodenhof:circleimageview:2.2.0'

        // implement image cropper
        implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.0'

        // implement image compressor
        implementation 'id.zelory:compressor:2.1.0'

        // implement test
        testImplementation 'junit:junit:4.12'
    //    implementation 'io.reactivex.rxjava2:rxjava:2.1.5'
        implementation 'io.reactivex.rxjava2:rxjava:2.2.0'
    //    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    //    androidTestImplementation …
Run Code Online (Sandbox Code Playgroud)

android rx-java rx-java2 android-studio-3.0

5
推荐指数
1
解决办法
2479
查看次数

Observable&lt;T&gt;.subscribe 上的 Kotlin 扩展功能不起作用

我正在尝试为 Observable.subscribe 编写一个自动记录错误的扩展函数。

fun <T> Observable<T>.subscribeAndLogE(onNext: Consumer<in T>): Disposable =
    subscribe(onNext, ErrorConsumer())
Run Code Online (Sandbox Code Playgroud)

ErrorConsumer 工作并可能记录错误,但subscribeAndLogE不像那样接受 lambdas .subscribe()。

observer.subscribe { 
                //works
           }

observer.subscribeAndLogE { 
                //does not work
           }
Run Code Online (Sandbox Code Playgroud)

它说:

错误

该 OnboardingScreen 是T通常的值。

我看不出原来Consumer<in T>在Observable做什么特别的接受lambda表达式。我在这里做错了什么?

kotlin rx-java rx-java2

5
推荐指数
1
解决办法
875
查看次数

Rx 运算符。忽略直到发出下一个

在我的应用程序中,我有一些耗时的逻辑,可以通过多种方式启动,比如自动或由用户手动启动。

// Let's describe different event sources as relays
val autoStarts = PublishRelay.create<Unit>()
val manualStarts = PublishRelay.create<Unit>()
val syncStarts = PublishRelay.create<Unit>()

// This is my time consuming operation.
fun longOperation() = Observable.interval(10, TimeUnit.SECONDS).take(1).map { Unit }

val startsDisposable = Observable
        .merge(
                autoStarts.flatMap { Observable.just(Unit).delay(30, TimeUnit.SECONDS) },
                manualStarts
        )
        .subscribe(syncStarts) // merge emissions of both sources into one

val syncDisposable = syncStarts
        .concatMap {
            longOperation()
        }
        .subscribe(autoStarts) // end of long operation trigger start of auto timer
Run Code Online (Sandbox Code Playgroud)

启动继电器会产生许多排放。假设用户单击按钮进行手动启动,距离定时器自动启动还剩 5 秒。longOperation() …

java android kotlin rx-java reactivex

5
推荐指数
1
解决办法
670
查看次数