- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 RxJava/RxAndroid 比较陌生。之前我一直在使用 AsyncTask 来完成长时间运行的任务。我已经将我的大部分 AsyncTask 转换为 RxJava,但这个。我遇到的特殊问题是调用类似 AsyncTask 的 publishProgress(params);
在后台线程。我需要这样做来更新 ProgressBar
的进度。
首先这是AsyncTask中的代码
private static class AddBooksToDatabase extends AsyncTask<String, String, String> {
//dependencies removed
AddBooksToDatabase(AddBooksDbParams params) {
//Removed assignment codes
}
@Override
protected String doInBackground(String... strings) {
//Initializing custom SQLiteOpenHelper and SQLite database
File mFile = new File(mFolderPath);
int booksSize = getFilesInFolder(mFile).size();
String[] sizeList = {String.valueOf(booksSize)};
//The first publishProgress is used to set the max of the progressbar
publishProgress(sizeList);
for (int i = 0; i < booksSize; i++) {
//publishProgress with current item, current file
publishProgress(String.valueOf(i), getFilesInFolder(mFile).get(i).getName());
//Inserting current items in database. Code removed
}
return null;
}
@Override
protected void onPreExecute() {
//Show ProgressBar
}
@Override
protected void onPostExecute(String s) {
//Hide ProgressBar
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
if (values.length == 1) {
//The first call to publishProgress
mProgressBar.setMax(Integer.parseInt(values[0]));
} else {
//Subsequent calls to publish progress
Log.i(TAG, "Current item is " + values[0] + " and current file is " + values[1]);
infoText.setText(values[1]);
mProgressBar.setProgress(Integer.parseInt(values[0]), true);
}
}
@Override
protected void onCancelled() {
cancel(true);
}
}
使用 RxJava 编写代码
final Observable<String[]> addBooksObserver = Observable.create(new Observable.OnSubscribe<String[]>() {
@Override
public void call(Subscriber<? super String[]> subscriber) {
subscriber.onNext(setAddSubscription());
subscriber.onCompleted();
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
private String[] setAddSubscription() {
S//Initializing custom SQLiteOpenHelper and SQLite database
File mFile = new File(mFolderPath);
int booksSize = getFilesInFolder(mFile).size();
String[] sizeList = {String.valueOf(booksSize)};
//The first publishProgress is used to set the max of the progressbar
addBooksObserver.doOnNext(addReturnParams(String.valueOf(sizeList.length), null, null));
for (int i = 0; i < booksSize; i++) {
EpubReader reader = new EpubReader();
//publishProgress with current item, current file*
addBooksObserver.doOnNext(addReturnParams(String.valueOf(sizeList.length),
String.valueOf(i), getFilesInFolder(mFile).get(i).getName()));
//Inserting current item in database. Code removed
}
return null;
}
private String[] addReturnParams(String totalItems, String currentItem, String currentFile) {
return new String[]{totalItems, currentItem, currentFile};
}
问题是 addBooksObserver.doOnNext(addReturnParams(
行显示此错误 doOnNext (rx.functions.Action1) 不能应用于 (java.lang.String[])
我不知道如何解决这个问题,因为我认为 setAddSubscription()
和 addReturnParams(String totalItems, String currentItem, String currentFile)
是返回 String 数组,那么这应该不是问题。你能帮帮我吗?
最佳答案
您只需将值传递给订阅者
的onNext
方法,不是doOnNext
方法你的可观察性!
您还必须订阅该服务。为您的观察者尝试这样的事情:
Observable.create(new Observable.OnSubscribe<String[]>() {
@Override
public void call(Subscriber<? super String[]> subscriber) {
setAddSubscription(subscriber);
subscriber.onCompleted();
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<String[]>() {
@Override
public void onCompleted() {
// handle 'oparation is done'
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String[] values) {
if (values.length == 1) {
//The first call to publishProgress
mProgressBar.setMax(Integer.parseInt(values[0]));
} else {
//Subsequent calls to publish progress
Log.i(TAG, "Current item is " + values[0] + " and current file is " + values[1]);
infoText.setText(values[1]);
mProgressBar.setProgress(Integer.parseInt(values[0]), true);
}
}
});
您还需要稍微修改一下您的私有(private)方法:
private void setAddSubscription(Subscriber<? super String[]> subscriber) {
//Initializing custom SQLiteOpenHelper and SQLite database
File mFile = new File(mFolderPath);
int booksSize = getFilesInFolder(mFile).size();
String[] sizeList = {String.valueOf(booksSize)};
//The first publishProgress is used to set the max of the progressbar
subscriber.onNext(addReturnParams(String.valueOf(sizeList.length), null, null));
for (int i = 0; i < booksSize; i++) {
EpubReader reader = new EpubReader();
//publishProgress with current item, current file*
subscriber.onNext(addReturnParams(String.valueOf(sizeList.length),
String.valueOf(i), getFilesInFolder(mFile).get(i).getName()));
//Inserting current item in database. Code removed
}
}
private String[] addReturnParams(String totalItems, String currentItem, String currentFile) {
return new String[]{totalItems, currentItem, currentFile};
}
关于android - 如何在 rxJava 中手动调用 observer.onNext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41263720/
我有两个问题: 'bind(onNext:' 和 'subscribe(onNext:' 有什么区别? struct Info { var index: Int? var data:
在一个使用 RxJava 2 的 Android 项目中,我在初始 Activity 的 onCreate 中创建了一个像这样的 Flowable: Flowable.create(new MyFlo
我有一个函数可以从网络服务中获取一些信息: const rxjs = require('rxjs'); const request = require('request'); const moment
所以我有一个Observable。我想知道什么时候 每个 下游订阅者 onNext 调用将要进行,这既是为了测量每个下游订阅者花费的时间量,也是为了背压目的。 Observable.lift 让我完成
我是 Rx 的新手,我真的很感激在错误处理方面的一些帮助。我有以下代码,它基本上是预先输入的: var observable = Rx.Observable.fromEvent(searchField
有没有办法延迟某些值的onNext?,例如: let subject = new Rx.BehaviorSubject({value: 100}) subject.onNext({ value: 12
下面我有向两个 API 端点发出请求的代码。我正在寻找一种让 onNext() 函数仅触发一次的方法。现在,如果您运行下面的代码,您可以看到它 logs fire 1,然后是 fire 2。我只想让它
我有一个发出长值的可观察值。一旦我收到一个>0的长值,那么我只需要中断发出并点击onComplete。有没有比我所做的黑客方法更好的方法来做到这一点 long id = -1l; methodA()
我正在尝试使用 RxJava 创建模型的代理,该代理允许其他人订阅模型中的更改。 这是我的代理的简化版本: class MyModelProxy { private static MyMode
我正在用 Retrofit 调用一个 url,我不关心结果。无论如何要忽略 onNext 方法吗? 目前我正在使用: getRetrofit().create(UserAPI.class)
我正在使用 RxAndroid 库来处理使用订阅者/可观察模式的项目列表。我的问题是,当一个项目被处理时,有一个进度条需要更新。但是在处理了 16 个项目之后,似乎 observable 没有调用 o
我有一个创建新 Observable 的用例: Observable.create(new Observable.OnSubscribe() { @Override public void
PublishSubject 非常适合在观察者和可观察对象之间架起一座桥梁: Observable observable = Observable.just("string"); Pu
有没有更简洁的方法来执行以下操作? 我有一个 Android observable 可以消除请求。 onNext 它调用第二个可观察对象。 {// when creating the android
我想在调用 onNext 后 10 秒发出一个随机值。所以 onNext 将被调用 12345,10 秒后 onNext 应该被调用以获得一个随机数。使用 RxJava 实现此目的的最佳方法是什么?提
每隔一段时间我都会用特定的查询检索推文。这些推文必须传递给计算和操作这些推文的服务。所以这些服务订阅了我的出版商。所以 publisher.hasSubscribers() 返回 true。但是提交或
我正在尝试在我的 http 代码中使用一些 reactiveX。为此,我创建了一个主题,并尝试调用 onNext 函数,但我收到一条错误消息,指出 subject.onNext 不是一个函数。 htm
我有这个简单的 retrofit2 api 接口(interface),其中包含 interface Api { @GET(BuildConfig.END_POINT) Observable
有人可以告诉我这个插件是如何工作的 http://bootstraptour.com/api/ ?我有一个步骤,我想重定向到动态网址。所以我编写了 onNext 回调,如下所示的代码。重定向工作正常,
我在 GRPC 文档中找不到任何与此相关的内容。 GRPC 是否期望我的 StreamObserver.onNext() 实现是非阻塞的?如果 GRPC 确实阻塞(例如拒绝新请求、对新请求排队等),会
我是一名优秀的程序员,十分优秀!