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

关于Subscriber.onStart()何时被调用的问题 #11

Open
lisuperhong opened this issue Jul 8, 2016 · 4 comments
Open

关于Subscriber.onStart()何时被调用的问题 #11

lisuperhong opened this issue Jul 8, 2016 · 4 comments

Comments

@lisuperhong
Copy link

lisuperhong commented Jul 8, 2016

大大你好,你写的文章上说Subscriber.onStart()总是在 subscribe所发生的线程被调用,但下面的代码并没有报错:
String baseUrl = "https://api.douban.com/v2/movie/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();

    ClientApi clientApi = retrofit.create(ClientApi.class);
    clientApi.getTopMovies(0, 5)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<MovieEntity>() {

                @Override
                public void onStart() {
                    progressBar.setVisibility(View.VISIBLE);
                }

                @Override
                public void onCompleted() {
                    progressBar.setVisibility(View.GONE);
                    Toast.makeText(context, "完成网络请求!", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onError(Throwable e) {
                    text.setText(e.getMessage());
                }

                @Override
                public void onNext(MovieEntity movieEntity) {
                    text.setText(movieEntity.toString());
                }
            });

我使用subscribeOn设置subscribe()在IO线程,直接在onStart()方法中显示progressBar,按理说progressBar没有在主线程执行,应用会奔溃才对,但是一切正常

@ShanZha
Copy link

ShanZha commented Jul 18, 2016

onStart()应该是跟随你当前线程的,如果你当前在主线程的话,肯定不会异常

@loulousky
Copy link

progress是貌似是可以在线程中使用的唯一的一个VIEW

@loulousky
Copy link

RXJAVA 可以指定ONSTART的使用线程

@mattxzhang
Copy link

确实有这个问题,是什么原因,指定是子线程,但onStart也可以显示dialog或者其他的loading

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

4 participants