【问题标题】:Observable seems to not call onNext after a while一段时间后,Observable 似乎没有调用 onNext
【发布时间】:2016-01-18 15:24:26
【问题描述】:

我正在使用 RxAndroid 库来处理使用订阅者/可观察模式的项目列表。 我的问题是,当处理一个项目时,有一个进度条需要更新。但是在处理了 16 个项目之后,似乎 observable 没有调用 onNext 方法,直到处理完其余的项目(90)然后调用了 90 次 onNext 方法。为什么会这样?这可能是内存问题吗?

代码如下。

订阅者:

public void startSingleRecognition(int id, int position) {
    mAdapter.updateItemProgress(0, position);
    Uri imageUri = Uri.parse(getHpCard(id).getUrlImage());
    final int[] i = {0};
    mSubscription = mRecognitionUtils
            .getRecognitionObservable(imageUri, configurations)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    abbyResult -> mAdapter.updateItemProgress(++i[0], position),
                    e -> e.printStackTrace(),
                    () ->  mAdapter.updateItemProgress(-1, position));

}

可观察:

public Observable<AbbyResult> getRecognitionObservable(Uri imageUri,
        ArrayList<Configuration> configurations) {
    return Observable.from(configurations)
            .flatMap(
                    configuration -> Observable.just(recognize(imageUri, configuration, this)));
}

recognize 方法处理图像很费力,我的第一个想法是这个方法消耗大量内存,并且 observable 无法将处理过的项目交付给订阅者,直到所有方法调用完成。但我不太确定,谁能证实这一点?

谢谢!

【问题讨论】:

    标签: android arraylist observable rx-android subscriber


    【解决方案1】:

    好吧,我想我已经解决了!问题是使用 flatMap 而不是 concatMap。这里解释得很好:http://fernandocejas.com/2015/01/11/rxjava-observable-tranformation-concatmap-vs-flatmap/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-05
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-29
      相关资源
      最近更新 更多