A “hot” Observable may begin emitting items as soon as it is created, and so any observer who later subscribes to that Observable may start observing the sequence somewhere in the middle. A “cold” Observable, on the other hand, waits until an observer subscribes to it before it begins to emit items, and so such an observer is guaranteed to see the whole sequence from the beginning.官网上的解释已经比较详细了,简单点说区别就是cold会等到有observer监听之后,才会发出事件,而hot则可能随时发出事件,不顾是否有observer监听了这个observable。

这是observable继承关系的类图,cold observable属于ordinary observable,也就是图中的observable类,而hot observable则属于ConnectableObservable,值得注意的是在ConnectableObservable有一个有趣的函数connect函数,方法签名如下:
从方法的注解可以了解,这就是hot和cold observable的区别所在,还有一点值得注意的就是类图中的Subject,Subject在RX中属于既是observable又是observer,具有双面性
本文深入探讨了RxJava中的Hot和Cold Observable的区别,包括它们的工作原理、继承关系及核心函数,同时介绍了ConnectableObservable类和Subject的概念。重点阐述了如何通过publish函数将Cold Observable转换为Hot Observable。
1365

被折叠的 条评论
为什么被折叠?



