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 are the core knowledge points of Rx.Net

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail what are the core knowledge points about Rx.Net, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

What is Reactive Extensions (Rx)

Rx is a functional programming class library that refers to observer and iterator design patterns for asynchronous consumption of data generated by observable objects. With Rx, developers will use the LINQ operator to manipulate the asynchronous data flow and the scheduler to parameterize concurrency in the asynchronous data flow, simply put, Rx = Observables + LINQ + Schedulers.

Using Rx requires Nuget to install the System.Reactive Nuget package

Usage scenarios of Rx

Responsive UI

On the UI interface, the user makes a keyword query on a control that binds a data collection. The regular process is that we have to wait for the user's keyboard to press the specified completion key (such as enter) or the mouse to click the query button before the program starts to execute the corresponding query processing. But suppose the requirement changes: "the user wants to bind the query result set corresponding to the keyword to the control in time after each keyword is entered." if faced with this requirement, how would you implement it? You will have to define the corresponding global status field and refresh it at the corresponding time interval. I'm sure the written code will bother you, too. In fact, you have a better choice, that is our protagonist Rx.

Rx core

Rx has two core interfaces IObservable and IObserver

IObservable

Let's first take a look at the structure of this interface:

The IObservable interface provides a Subscribe (subscription) method, and the input parameter is an observer object interface.

We can call IObservable the observed (observable) and IObserver the observer.

Through the interface signature, we can see that the observer needs to output T-type objects. Need to understand the observed IObservable we need to compare with some existing conventional knowledge points, here we use IEnumerable comparison.

I think we've all used Linq and manipulated the IEnumerable collection, and the obvious state of the IEnumerable collection is that the elements it stores are static. Unless the state of the elements in the collection is added or deleted or modified as shown in the code, the collection is basically static (data unchanged). But IObservable is different, its elements change according to the data provided by the observer (unpredictable), just as you can't predict the behavior of users on UI.

The following table shows the difference between the two.

IEnumerable

It is convenient to enumerate the values of collection elements.

IObservable

The value of an observable object.

IObserver

The IObserver interface can be understood as consumption is carried out as an interface to provide data, and its three methods determine the trend of the observation behavior of this data flow.

The popular understanding is that data is generated by the observer and consumed by the observer.

Let's take a look at the structure of IObserver

OnNext means to consume new data.

OnError means to observe an exception in the data flow.

OnCompleted explicitly closes the observation data stream.

Code example

The following code defines an observable queue that provides the observer with three int type input parameters 1, 2, and 3 for the observer object's OnNext method consumption. The MyConsoleObserver (observer) prints out the data after it is obtained.

Through the sample code, we know the data flow subscription and consumption process of Rx.Net.

Subject

Let's recognize that Subject,Subject is an IObservable that generates a value in the form of a command and pushes it to the observer object. Let's look at the structure of Subject.

Look at the inheritance relationship. Let's continue to see what's in SubjectBase.

Alas, this class is really amazing, inheriting both IObserver and IObservable. You can provide your own data for subscription and consumption.

Let's take a look at how Subject works:

The execution result is as follows:

It should be noted that the subscription method needs to be declared before the data is generated.

What are the core knowledge points about Rx.Net to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report