使用RxJava我有一个Observable<A>和一个Observable<B>.我想在发出B第一个(也是唯一的)元素时立即开始订阅A.我知道我可以像这样链接它:
final Observable<A> obsOfA;
final Observable<B> obsOfB;
obsOfA.subscribe(new Action1<A>() {
@Override
public void call(A a) {
obsOfB.subscribe(...)
}
});
Run Code Online (Sandbox Code Playgroud)
..但这会导致嵌套语法,一旦我们介绍就会变得丑陋Observable<C>.如何将语法"解包"到更流畅的语言 - 获得更像javascript Promise.then()-flow 的语法?
我正在使用RxJava,Observable里面有多个项目.我想要做的是在第一项上运行函数A,在所有项上运行函数B,在Observable完成时运行函数C :
-----1-----2-----3-----|-->
| | | |
run A | | |
| | | |
run B run B run B |
|
run C
Run Code Online (Sandbox Code Playgroud)
是否有一种聪明的方式来表达lambda函数?我已经有了以下解决方案,但它看起来很难看,我怀疑有更好的方法可以做到这一点:
observable.subscribe(
new Action1<Item>() {
boolean first = true;
@Override
public void call(Item item) {
if (first) {
runA(item);
first = false;
}
runB(fax1);
}
},
throwable -> {},
() -> runC());
Run Code Online (Sandbox Code Playgroud) 我正在努力了解RxJava.我的测试代码是:
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import java.util.concurrent.TimeUnit;
public class Hello {
public static void main(String[] args) {
Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
try {
Thread.sleep(1000);
subscriber.onNext("a");
Thread.sleep(1000);
subscriber.onNext("b");
Thread.sleep(1000);
subscriber.onNext("c");
Thread.sleep(1000);
subscriber.onNext("d");
Thread.sleep(1000);
subscriber.onNext("e");
Thread.sleep(1000);
subscriber.onNext("f");
Thread.sleep(1000);
subscriber.onNext("g");
Thread.sleep(1000);
subscriber.onNext("h");
} catch (InterruptedException e) {
subscriber.onError(e);
}
}
});
observable
.delay(2, TimeUnit.SECONDS)
.subscribe(new Action1<String>() {
@Override
public void call(String string) {
System.out.println(string);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
没有.delay(2, TimeUnit.SECONDS) …
我是RxJava的新手,并尝试围绕一个更复杂的登录逻辑,包含三个异步方法来处理.对我来说这是"如果我将这个东西转换为RxJava,任何东西(tm)都是可能的":)
所以我想做的是以下内容:
Call A -> (Process A) -> Call B with results of A -> (Process B) -\
\ -> Combine and Subscribe
\-> Call C with results of A -> (Process C) -/
Run Code Online (Sandbox Code Playgroud)
现在的问题是Call C分支应该只在特定条件下执行,否则不能执行(Combine and Subscribe然后可以NULL从该分支接收一个值,这没关系).
此外,错误处理并非易事:虽然Call A和Call C(如果这样运行)需要将错误传递onError给最终订阅者,但Call B"成功"是相当可选的,并且在失败的情况下可以忽略.
这是我到目前为止提出的,它仍然忽略了"C"分支:
mApi.callA(someArgs)
// a transition operator to apply thread schedulers
.compose(applySchedulers())
// from rxlifecycle-components, unsubscribes automatically
// when the activity goes down …Run Code Online (Sandbox Code Playgroud) 我正在阅读教程:
http://code.tutsplus.com/tutorials/getting-started-with-reactivex-on-android--cms-24387
特别是RxAndroid,但它和RxJava几乎相同.我不确定我完全理解这个概念.
下面我写了一个方法,然后是一个示例用法.
我的问题是:这是实现我的函数的正确方法,以便我可以异步地在其他线程上运行它们吗?实际上,它们只会返回一个运行真实代码的已创建的Observable,并处理错误和所有这些内容.
或者这是错的,那么我想知道正确的方法.
Observable<String> googleSomething(String text){
return Observable.create(new Observable(){
@Override
public void call(Subscriber<? super String> subscriber) {
try {
String data = fetchData(text); // some normal method
subscriber.onNext(data); // Emit the contents of the URL
subscriber.onCompleted(); // Nothing more to emit
} catch(Exception e) {
subscriber.onError(e); // In case there are network errors
}
}
});
}
googleSomething("hello world").subscribeOn(Schedulers.io()).observeOn(Schedulers.immediate()).subscribe(...)
Run Code Online (Sandbox Code Playgroud)
还使用Schedulers.immediate()来执行当前线程上的订户代码?它说"创建并返回一个在当前线程上立即执行工作的调度程序." 在javadoc,但我不确定.
我试图习惯rxjava,我试图QuoteReader在Observable中调用下面的内容.我不知道如何处理抛出的异常,
public class QuoteReader {
public Map<String, Object> getQuote() throws IOException{
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url("http://quotes.rest/qod.json").build();
Gson gson = new Gson();
Map<String, Object> responseMap = null;
try(Response response = okHttpClient.newCall(request).execute()) {
responseMap = gson.fromJson(response.body().string(), Map.class);
System.out.println("response map : "+responseMap);
} catch(IOException ioe) {
ioe.printStackTrace();
throw ioe;
} finally {
okHttpClient = null;
request = null;
}
return responseMap;
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我想写的rx代码,
rx.Observable.just(new QuoteReader().getQuote()) //compile time error saying unhandled exception
.subscribe(System.out::println);
Run Code Online (Sandbox Code Playgroud)
我应该如何更新代码来处理异常.谢谢!
我试图顺序运行2个observable,但是,如果第一个给出一个特定的错误,我想停止concat并执行另一个东西.
尽管如此,第一个observable可能会给出很多异常,我只想在达到特定错误时停止传播concat(在本例中为HttpException 403),另一方面我想在没有任何异常时继续执行concat执行403 HttpException.
我已经完成了以下代码,但到目前为止,我无法继续执行concat执行,将当前的observable替换为Observable.error(throwable).
有没有一种方法可以在不改变可观察逻辑或用户逻辑的情况下完成它?或者某些东西停止传播特定错误的concat,并在给出其他错误时继续?
Observable.concat(getObservable1(), getObservable2())
.onErrorResumeNext(new Func1<Throwable, Observable<? extends OperationModel>>() {
@Override
public Observable<? extends OperationModel> call(Throwable throwable) {
if(throwable instanceof HttpException && ((HttpException)throwable).code() == 403) {
return Observable.error(throwable);
}
return Observable.empty(); //here I just want to proceed the concat despite giving an error
}
.subscribeOn(Schedulers.io()) //execute requests should be on io() thread
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new CustomRequestSubscriber<>());
Run Code Online (Sandbox Code Playgroud) 根据我的观点.我认为share()是相同的publish().autoConnect().但在这段代码中结果并不相同
Observable<Integer> cold = Observable.create(subscriber -> {
for (int i = 0; i <= 2; i++) {
System.out.println("Hot Observable Emit " + i);
subscriber.onNext(i);
}
});
ConnectableObservable<Integer> connectble = cold.publish().autoConnect(2);
//Obserable(Integer) connectavle = cold.share();
connectble.subscribe(subscriber1);
connectble.subscribe(subscriber2);
Run Code Online (Sandbox Code Playgroud)
该 publish(). autoConnect() output
Hot Observable Emit 0
Subscriber 1 : 0
Subscriber 2 : 0
....
Run Code Online (Sandbox Code Playgroud)
该 share() output
Hot Observable Emit 0
Subscriber 1 : 0
//the subscriber2 not receive event
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我们不能share()在rx-java中使用多播?我找到了一个结论但不适合这种情况.
测试环境:oracle jdk1.8 …
有没有办法计算RxAndroid中已经在流中处理过的元素数量?
我有类似的东西:
Observable.fromArray(new String[]{"these", "are", "my", "values", "."})
.map(s -> doSomeCoolStuff(s))
// ...
.subscribe(amountOfProcessedItems -> Log.d("test", "" + amountOfProcessedItems));
Run Code Online (Sandbox Code Playgroud)
我正在寻找一些东西,以便我的输出看起来像1 2 3 4 5,基本上每个项目后计算已经发出的项目数量.
我正在为我的应用制作启动画面。我希望它至少持续N秒,然后才能转到主屏幕。
我有一个Rx变量myObservable,它从服务器或本地缓存中返回数据。如何强制myObservable在完成至少N秒?
myObservable
// .doStuff to make it last at least N seconds
.subscribe(...)
Run Code Online (Sandbox Code Playgroud)