Giter Club home page Giter Club logo

Comments (6)

dhhAndroid avatar dhhAndroid commented on July 21, 2024

你好,缓存的Observable是已经在主线程了,应该不会出现更新UI导致崩溃的问题,这个版本的代码,我在自己公司的项目是使用的,没有出现类似问题;是不是在RxWebSocket.get()后,做了操作符变换,有的操作符变换,默认就在计算线程,不是UI线程,这也会导致subscribe之后的线程不在主线程,这个问题,你可以在单独订阅后可再做一下切换到主线程的操作。

from rxwebsocket.

khejing avatar khejing commented on July 21, 2024

谢谢回复!我的意思是observable.startWith()返回的是一个新的Observable对象,原Observable对象指定的observeOn()已经没用了。这个对象如果不指定subscribeOn()或observeOn()的话,默认subscribe()方法在哪个线程调用,subscribe()方法的参数回调就会在哪个线程执行

from rxwebsocket.

dhhAndroid avatar dhhAndroid commented on July 21, 2024

嗯嗯,一个很好的建议,我查看了一下startWith的操作符注释,startWith does not operate by default on a particular Scheduler.说的是startWith不会改变原来Observable执行的线程,也就是说原来的Observable已经指定onserveOn(mainThread())主线成后,startWith之后的Observable也是在主线程。你可以写下测试代码验证一下,如果还是不放心,你就在调用的时候,在订阅前,再指定一下主线程吧。

from rxwebsocket.

dhhAndroid avatar dhhAndroid commented on July 21, 2024

RxJava相关问题,可以加入项目下的QQ群,大家一起交流学习,非常感谢反馈问题!

from rxwebsocket.

khejing avatar khejing commented on July 21, 2024

测试代码:

    var observable: Observable<Int>? = null
    fun getMyObservable(): Observable<Int> {
        if (observable == null) {
            observable = Observable.just(1).observeOn(Schedulers.io())
        } else {
            observable = observable!!.startWith(2)
        }
        return observable!!
    }

    @Test
    fun rxjavaTest() {
        Observable.just(Unit)
                .observeOn(Schedulers.computation())
                .subscribe { _ ->
                    getMyObservable()
                            .subscribe {
                                println("first got data $it ${Thread.currentThread().name}")
                            }
                    getMyObservable()
                            .subscribe { d ->
                                println("second got data $d ${Thread.currentThread().name}")
                            }
                }
        Thread.sleep(10 * 1000)
    }

输出为:

first got data 1 RxCachedThreadScheduler-1
second got data 2 RxComputationThreadPool-1
second got data 1 RxCachedThreadScheduler-1

请问我的意思说清楚了么?

from rxwebsocket.

dhhAndroid avatar dhhAndroid commented on July 21, 2024

最新版本已经修复

from rxwebsocket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.