我正在使用RxAndroid在后台做一些东西。这是我的代码:RxAndroid和多线程
Observable<MyClass[]> observable = Observable.create(new Observable.OnSubscribe<MyClass[]>() {
@Override
public void call(Subscriber<? super MyClass[]> subscriber) {
System.out.println(Looper.myLooper() + " - " + Looper.getMainLooper());
try {
MyObject myObject = ...
//do the background work
subscriber.onNext(myObject);
subscriber.onCompleted();
} catch (Exception e) {
subscriber.onError(e);
e.printStackTrace();
}
}
});
observable.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<MyClass[]>() {
@Override
public void call(MyClass[] myObjects) {
//do work on the ui Thread
}
}
);
这是我第一次使用RxAndroid/RxJava/Looper.myLooper()/Looper.getMainLooper()
从什么,我说,Looper.myLooper()
为您提供了线程的名称标识当前的代码运行并Looper.getMainLooper()
给你的ID的主线程。当我运行应用程序时,在SysOut
中,它会为它们打印出相同的ID。
我做错了什么或者我误解了2 Looper功能?
嘿。抱歉。已经离开了一段时间。我今晚会尝试一下,回到你身边:) –