What is RxSwift? Part 3 – Transforming Observable Sequences

Tram Ho

The third part of this RxSwift series. In the previous article, we looked at different types of Audience with operational examples to see how they handle playback and stop events. As of now, creating and registering strings seems like a fairly simple task, so let us dive into another level of complexity and start converting strings.

What Does It Mean To Transform A Sequence?

Often you would register a string just to receive an element type different from the type you want to work with. For example, you might want to work with a String representation of a Day, instead of getting elements of the Date type in your subsequent events, you’ll want to convert your string so that it sends you a type. Chain. Sound familiar? We have dealt with these types of transformations in standard collection types using methods such as map, FlatMap, minify and filter. We will see similar names, but because reactive programming is an asynchronous model, the behavior of the response operators may have some surprises. In this article, we will look at the map, FlatMap and FlatMapLatest with checking their marble diagram and an coding example for each map.

Map And An Introduction to Marbles

The map is a nice, simple place to start because its behavior is almost the same as for collections. It goes through each element, applies a transformation, and returns a new string with the modified elements. As we expected, the original sequence remains unchanged. So far we have been able to describe response operators without the help of visual diagrams, but now we start getting into territories where they can be really useful, so , without delay, let me introduce you to the marbles. You can see the name of the operator and the transformation applied in the center rectangle. Horizontal lines show changes in the series over time. In this case, we have an input sequence above the operator and an output sequence below. For the map operator, each time the input sequence issues a subsequent event, the element is converted and a new value is generated for the output string.

As in the code:

In this example, we can see that we create a string of type Int (line 3) and map it so that the values ​​of the elements are squared (line 5). The most important thing to note is that the original string remains unchanged and a new string is output which means we currently have two strings (seqOrig and seqMapped). We can then register both strings (lines 7 and 12). When subsequent events are emitted in seqOrig (lines 17, 19), they are converted and emitted in mapped sequences. As we expected, both subscriptions are handled by the dispose bag when the scope is exited.

FlatMap

We have seen how easy it is to apply transformations to elements, but if we want to access an observable attribute within the element, or what happens if is the element itself observable? For this, we need to use flapMap. In this diagram diagram, note that the first element O1 in the input sequence is transformed and the result is output to the output string. But interestingly, when the same element emits a subsequent event some time later, it is also converted and emitted into the output sequence. In other words, output chains are registered to observable elements and will receive subsequent events in those elements whenever they occur. This atypical is very different from what we will find in a traditional FlatMap.

In this example, we want to observe the food items that a group of people eat. To begin with, we created an entity called Person, notably its attribute. Eatenen things (eg 1, line 4). As you will remember, a published topic does not replay any historical information, so it will simply emit a subsequent event when a person eats something. To support this post, we can create a convenient feed (item: String) (example 1, line 10)

With the entity set up, we can create two people: Matt and Shawna (example 2, line 1 Difference 2) and one can observe, so that we can register the eating habits of the element. human (example 2, line 6). At this point, we use FlatMap so we can access the observable .thingsEaten in our human element. You can also recall asObservable () to access the BehaviorSubject inside our Variables wrapper (for example 2, line 8).

Wrap Up

In today’s article, we looked at transformation chains and in doing so, we learned to not only transform the elements, but also transform the observations in our elements. Of course, there are more response operators on this topic and I hope that you will take some time to explore them. If there is a large sum of money that I hope you will get, it is the idea that you can understand a lot about new operators by studying their diagrams. They are widely used in Reactive documentation and even have interesting interactive diagrams that you can play with. Referent from Source: Link

Share the news now

Source : Viblo