我想保留我的主线程,直到RxJava 2 Observable完成。我的编码就像这里一样。这种情况是当我请求方法包含下面的代码时,方法只是执行而不等待可观察的
Observable
.fromArray(observableList)
.subscribeOn(Schedulers.io())
.subscribe(new Consumer<List<Observable<List<String>>>>() {
@Override
public void accept(List<Observable<List<String>>> list) throws Exception {
for (Observable<List<String>> observable : list) {
System.out.println("Thread name " + Thread.currentThread().getName());
observable.subscribe(new Consumer<List<String>>() {
@Override
public void accept(List<String> t) throws Exception {
Gson jsonBuilder = new Gson();
Object obj = new Object();
JsonElement element = jsonBuilder.toJsonTree(obj);
element.getAsJsonObject().addProperty(t.get(0), t.get(1));
Gson g = new Gson();
Object out = g.fromJson(t.get(1), Object.class);
microResponses.put(t.get(0), out);
}
});
}
}
});
答案 0 :(得分:0)
使用以下任何一项: - 1.删除subscribeOn 2.使用Thread.sleep足够长的时间来完成任务 3.谷歌到rxjava中的阻止用法
答案 1 :(得分:0)
您不需要多级订阅,也不需要嵌套Observable:
Oservable
.fromArray(observableList)
.flatMapIterale(i -> i)
.flatMap(i -> i)
.doOnNext(t -> {
Gson jsonBuilder = new Gson();
Object obj = new Object();
JsonElement element = jsonBuilder.toJsonTree(obj);
element.getAsJsonObject().addProperty(t.get(0), t.get(1));
Gson g = new Gson();
Object out = g.fromJson(t.get(1), Object.class);
microResponses.put(t.get(0), out);
})
// .subscribe(...) or .doOnComplete(...).subscribe();
;
但是,您编写的代码有几个问题:
new JsonObject()
?Object out = g.fromJson(t.get(1), Object.class);
除了空JsonObjecy外不会产生任何内容,因为Object
没有任何属性。Map.Entry<String,Object>
,甚至是Pair<String, Object>
?