Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the RxJava threading model?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "what is the RxJava threading model". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Get started with experience

Switching threads in RxJava is very simple, such as the most common asynchronous thread processing and the main thread callback model, which can be elegantly handled with the following code:

Observable.just ("magic") .map (str-> doExpensiveWork (str)) .subscribeOn (Schedulers.io ()) .subscrieOn (AndroidSchedulers.mainThread ()) .subscribe (obj-> System.out.print (String.valueOf (obj)

As mentioned above, subscribeOn (Schedulers.io ()) ensures that the doExpensiveWork function occurs in the Io thread, and observeOn (AndroidSchedulers.mainThread ()) ensures that the subscribe callback occurs in the main thread of Android. So, this naturally leads to the key point of this article, what is the difference between subscribeOn and observeOn?

Process analysis

To answer the above questions, we first need to have a general understanding of the process of RxJava. From the generation of an Observable to the final implementation of the subscribe, there can be n changes, each of which will produce a new Observable. Just like the torch relay at the opening of the Olympic Games, each torch will be passed to the next person, and the torch will eventually be lit by a torch bearer, that is, an Observable will eventually perform the subscribe operation. There must be a connection between each Observable. This relationship is reflected in the code that each transformed Observable holds a reference to the OnSubscribe object in the last Observable (parameters required by the Observable.create function). Finally, the key code in the subscribe function of Observable is this sentence:

Observable.onSubscribe.call (subscriber)

This observable is a transformed observable, so who is this onSubscribe object? How an observable directly executes subscribe without any transformation, of course, it is the onSubscribe we passed in create, but if we go through map, reduce and other transformations, this onSubscribe should obviously be the parameter passed by the observable after the transformation is created, and most of the transformations will eventually be handed over to the lift function:

Public final Observable lift (final Operator

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report