Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RxJava的其他线程中可以进行UI操作吗? #1

Open
Harto-Lin opened this issue Jun 2, 2016 · 4 comments
Open

RxJava的其他线程中可以进行UI操作吗? #1

Harto-Lin opened this issue Jun 2, 2016 · 4 comments

Comments

@Harto-Lin
Copy link

Harto-Lin commented Jun 2, 2016

observeOn(Schedulers.newThread())
.map(new Func1<Integer, String>() {
@OverRide
public String call(Integer integer) {
textView.setText(txt_thread_log.getText() + "\n" + "Schedulers.newThread() thread id =" + Thread.currentThread().getId());
return "5" + integer;
}
})
这其中的UI操作是在新的线程中操作吗,而且UI操作不是只能在主线程中操作吗?这搞得我好焦灼啊

@Harto-Lin Harto-Lin changed the title RxJava的其他线程中可以进行UI操作码? RxJava的其他线程中可以进行UI操作吗? Jun 2, 2016
@YongHuiLuo
Copy link
Owner

YongHuiLuo commented Jun 13, 2016

ReactiveX/RxJava#3605
看看这个issues能不能解决你的问题,我也产生过相同的疑问,里面有RxAndroid创建者的回答。

@Harto-Lin
Copy link
Author

这个原因是什么?直是说更深一步的学习,有什么可以推荐的吗?

@zsxwing
Copy link

zsxwing commented Jun 14, 2016

Android要求UI操作必须在主线程中,但是不代表所有的地方都会检查。如果把UI操作放到别的线程,可能会抛出异常,也可能导致一些多线程的race condition。

@zsxwing
Copy link

zsxwing commented Jun 14, 2016

这其中的UI操作是在新的线程中操作吗,而且UI操作不是只能在主线程中操作吗?这搞得我好焦灼啊

这段代码的textView.setText是在新创建的线程中执行的。更明确地说,UI操作在哪个线程跑是用户自己控制的,Android并不能帮助你做这件事。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants