Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

android - 使用Firebase InstanceID和RxJava 2.0的异常(MAIN_THREAD)(Exception (MAIN_THREAD) using Firebase InstanceID and RxJava 2.0)

Our application is getting the FirebaseInstanceId with the following code :

(我们的应用程序使用以下代码获取FirebaseInstanceId :)

Observable.create<ReactiveStreamResult> {
    it.onNext(FirebaseInstanceId.getInstance()
       .getToken(SENDER_ID, FirebaseMessaging.INSTANCE_ID_SCOPE)
    )
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())

But for some device this code triggered an IOException (MAIN_THREAD).

(但是对于某些设备,此代码触发了IOException (MAIN_THREAD)。)

Is it possible that the creation of the observable is done on the MAIN_THREAD not on the IO Thread ?

(是否可以在MAIN_THREAD而不是在IO Thread上创建可观察对象?)

Can the defer operator solve this issue ?

(延迟运算符可以解决此问题吗?)

Thank you for your answer Best Regards

(谢谢您的回答最好的问候)

Guillaume

(纪尧姆)

  ask by Shindra translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The below example should not cause the issue, because you stream it correctly.

(下面的示例不应导致此问题,因为您可以正确地流式传输它。)

Observable.create<ReactiveStreamResult> {
    it.subscribeOn(Schedulers.io())
    it.onNext(FirebaseInstanceId.getInstance().getToken(SENDER_ID, FirebaseMessaging.INSTANCE_ID_SCOPE))
    it.observeOn(AndroidSchedulers.mainThread())
    it.subscribe { // Do what you need to do here for the main thread }
}

However, since I only see this bit of the code, I urge you to also read the below documentations to understand it correctly.

(但是,由于我只看到了这段代码,因此我敦促您也阅读以下文档以正确理解它。)

RxJava Schedulers

(RxJava调度程序)

Understanding RxJava subscribeOn and observeOn

(了解RxJava的SubscribeOn和ObservOn)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...