ReactiveSwift framework, reactiveswift

Source: Internet
Author: User

ReactiveSwift framework, reactiveswift

There are not many projects recently, so I have studied RxSwift and RAS. RAC has been used in previous projects. Here I will briefly introduce what RAS is.
Summary:
In RAC 5.0, the API has been renamed. The Swift-related part has been drawn out to form a new framework: ReactiveSwift. It can be seen that Apple's father loves the younger son and should vigorously promote swift, not just for Apple's development.

Changes:
1. RACSignal, SignalProducer, Signal
2. RACCommand and Action
3. racschedtype and SchedulerType
4. RACDisposable and Disposable

Introduction:
First, we use cocoapod to import pod 'reactiveswift'
Then import the header file to the project.
Import Result
Import ReactiveSwift

If you only import ReactiveSwift and do not import the Result, you will report an error when typing NoError, because NoError depends on the Result.

Main Types
1. Events)
An Event is represented as an Event type, which indicates that something has happened. In ReactiveSwift, events are the core of communication. An event may indicate that a button is pressed, a message is received from the API, or a long operation is completed. In any situation, events that have a certain meaning occur and are sent to [observer] through [Signal].
Event
Is an enumerated value, which can be expressed as a value event or any of the three terminated operations:
A. value event provides a value from the source.
B. An error occurs before the signal ends normally. This event is parameterized by the ErrorType type, which indicates that the event is allowed to fail. However, if a failure is not allowed, the event will provide the NoError type to prevent the failure.
C. The completion event indicates that the signal is successfully completed, and the source is not sending any other value.
D. Abort an event means that the operation may or may not be successful.


2. Signal)
A Signal, expressed as a [Signal] type, is a sequence of observed events.
Signals are usually used to indicate ongoing event streams, such as notifications and user input. As work progresses and data is accepted, events are sent based on signals, and signals can push events to any observer.
All the observers can see the event at the same time.
If you want to obtain an event, you must observe a signal. Observing a signal does not cause any side effects. In other words, the signal is completely producer-driven and push-based, and the consumer (observer) does not have any impact in their lifecycle. When observing signals, you can only evaluate them using the sequence in which they (events) are sent in the same order. We cannot random access signal values.
Signals can be controlled through the Application primitive [primitives]. For example, filter, map, and reduce are typical primitives that can operate on a signal. It is worth noting that primitive operations can only be performed on value events.
In the life cycle of a signal, it may have countless value events and end with a termination event (either a failure event, a completion event, or a suspension event ). Termination events cannot be included in signal values and must be processed separately.


3. Pipelines)

A pipeline is created by Signal. pipe () and is a Signal that can be manually controlled.

This method returns a signal and an observer. This signal can be controlled by sending events to the observer. This is very useful for connecting non-RAC code.

For example, when processing the application logic in the block callback function, these blocks can simply send events to the observer. At the same time, the question can return a signal and hide the implementation details of the callback.

4. Signal Generator (Signal Producers)
A signal generator can be expressed as a [SignalProducer] type, which can create signals and produce side effects.
They can be used to represent some operations or tasks, such as network requests. Each time start () is called, a new basic operation is created and callers are allowed to observe the results. The startWithSignal () variant can be used to access the signals generated. If necessary, it is allowed to be observed multiple times.
Because of the start () action, the signal created by the same generator may have events of different sequences or versions, and the stream may be completely different. Unlike a normal signal, it does not start to work (no event is generated) before adding an observer, and it will work again every time an additional observer is added.
When a signal generator starts, a [disposable] is returned, and the disposable is used to stop/Close the work related to the generated signal.
Just like a signal, a signal generator can also be controlled through primitive elements, such as map and filter. Each signal primitive can use the lift method to operate the signal generator. In addition, there are additional primitives that can control when and how, such as times.

5. Observer)
An observer can be anything as long as it is waiting or is about to wait for an event in the signal. In RAC, an Observer is represented as a [Observer] that can receive [events].
The observer is implicitly created by using the closure-based Signal. observe or SignalProducer. start method.

6. Actions)
An Action, expressed as the [Action] type. When an action is executed, it may generate 0 or more outputs, or it may fail.
Actions are useful for performing work that has side effects in user interaction, such as pressing a button. An action is automatically unavailable based on a [attribute], which can be expressed by disabling some operations in the UI.

7. Properties)
An attribute is represented as a [PropertyProtocol] type. It can store a value and notify the observer about the future change.
The current value of an attribute may be contained in the getter of this value. The generator getter returns a [Signal Generator], which sends the current value of the attribute, which changes over time. Signal getter
Not only will the initialization value be sent, but all changes will be sent over time.
<~ Operations can be used to bind attributes in different ways. In any case, the target is bound to the target, which is represented as [BingingTargetProtocol]. All variable attribute types are represented as [MutablePropertyProtocol], which is the internal binding target.
Property <~ Signal: binds a signal to an attribute. When this attribute value is updated, the signal sends the latest value of this attribute.
Property <~ Producer: Start a given signal generator and bind the attribute value to the latest value sent by the signal.
Property <~ OtherProperty: binds an attribute to another attribute. Therefore, once the value of the source attribute is updated, the value of the Target attribute is also updated.
Attribute provides a large number of transaction operations, such as map, combineLatest, or zip. These operations are similar to those in the signal and signal generator.

8. Disposables
A Disposable is represented as a [Disposable] Protocol, which is a mechanism for memory management and cancellation. When a signal generator is started, a disposable is returned. Disposable can cancel started work (such as background processing and network requests), clear all temporary resources, and then send a final stop event with a specific signal. Observe a signal and return a disposable. Processing it will prevent the observer from accepting future events from the signal, but it will not affect the signal itself.

9. scheduler (Schedulers)
A scheduler is represented as a [SchedulerProtocol] Protocol, which is a serial executable queue used to execute work or deliver (deliver) results. The signal and signal generator can be used to deliver programs on a specific scheduler. The signal generator can also be used to start their work on a specific scheduler. The scheduler is similar to the Grand Central Dispatch queue, but the scheduler supports canceling operations (through [disposables]) and always runs in serial mode. [ImmediateScheduler
] Is an exception. The scheduler does not support synchronous execution. This helps avoid deadlocks and encourages primitive operations on the signal and signal generator to replace closures.
The scheduler and NSOperationQueue are similar, but the scheduler does not allow tasks to be rescheduled or dependent on another task.

Main usage:
1. Signal Creation

Let's not talk about the code.

Giggling

The following is the printed result.


Next we will create a heat signal to see what the result is. what? You said you don't know what a hot signal is or a cold signal. Okay.

The hot signal is active, and even if you do not subscribe to the event, it will still push at any time. The cold signal is passive. It sends messages only when you subscribe to them.
A hot signal can have multiple subscribers, one to multiple, and the signal can share information with the subscriber. The cold signal can only be one-to-one. When there are different subscribers, the message will be completely sent.

Let's take a look at the hot signal. We don't need to talk much about it. You can see the code.


2.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.