Winklix is an app development company that does most of their iOS development using Reactive Swift. There are two approaches of solving a problem in swift. First one is the imperative way and second one is the functional reactive way. Reactive Swift is taking leaps and bounds in today’s world. It is getting more and more prevalent with each passing day. Let us try to understand what is all the fuzz about?
Basic building blocks of Reactive Swift
One of the best ways to manage the complexity of user interface is through reactive programming. Reactive Swift creators were focused to creating a language which is declarative, composable and flexible. The paramount feature of the Reactive Swift is that it is an amalgamation of streams, transformation and bindings. Streams are a very familiar data structure through which you can send anything. It is decisive to listen for the stuff, otherwise you may miss it. Using the transformation, you can basically map one type of array into another type of required array. Binding is a third ingredient of Reactive Swift. Through bindings, you can bind the stuffs to things.
Reactive Swift has a very basic building block structure given below which will assist you to write code in functional reactive way. These key building blocks are categorized in the following way.
Source
Our main focus is on the rapid changes over time rather than a particular state in time. These all primitive blocks are categorized under the Source category in reactive programming. Source comprises of Event, Signal, Signal Producer and Action. These key building blocks are accountable for the proliferation and occurrence of the changes in time.
- Event
An Event is the occurrence of something. Events are the centrepiece of communication. An Event might be occurrence of an error, press of a button or receive of any information from an API. Events are of following four types
- Value: This event offers a value from the source.
- Failed: This event means that an error has occurred before the completion of a signal.
- Completed: This event means that the signal has completed successfully and no more signals will be sent from the source.
- Interrupted: This event means that the signal has been interrupted because of the cancellation. This means that the operation was neither successful nor unsuccessful.
- Signal
Signals represent event streams that are in progress like user input, notifications etc. When the user gets the data, events are sent through signals. This is pushed out to all the observers due to which all observers can view the events at same time.
- Signal Producer
Signal Producer creates signals and represents tasks and operations like network requests etc. Signal Produces is considered to be cold as it needs to be initiated while Signal is considered as warm because it does not need to be initiated.
- Action
Actions are responsible for doing work when executed with an input like click of a button. It generates output values like zero while execution. Actions can also be disabled on property which will disable any controls associated with the action.
- Property
Property is used to store a value. If there is any change in the value, it automatically notifies. The current value can be accessed from the value getter. The current value is send by the producer getter through signal producer.
Consumer
The basic blocks in this category listens and perform according to the events generated and sent by source primitives. Consumer category has two sub-categories.
- Observer
It captures the work that needs to be performed in reply of the emitted events from the source. An observer takes event as an input.
- Mutable Property
Just like Property, it is also used to store the value. But it is different from the property as it can be muted directly. It has the ability to update its value according to the values received by signal.
Operators
Operators are the functions that transform signal and signal producer. These are composable primitives that work with event streams. These operators can be used for transformation, performing side-effects, flatten, combine etc.
Scope
These primitives are used for determining the interaction time between the consumer and source. It has the following two sub categories
- Disposable
Disposable enables the user to manage the memory and cancellation of signal. When we start the signal Producer, we get a disposable which can be used to cancel the started work.
- Lifetime
It determines the longevity of an object like we want to observe a notification till the UI component is on the screen.
The Flow
The complete flow of how the primitives complete the job is explained below
Step 1: Defining a Signal
let signal: Signal<String?, NoError>
Step 2: Transforming a Signal
As we can see that in the first step, we get an optional string. Then we need to transform the nil string to empty string and transform it one more time to give Boolean values. We have used skipNil to ignore nil strings here
let transformedSignal = signal
.map { text in
text ?? “”
}.map { text in
text.characters.count > 10
}
Step 3: Observing Signal
We observed that the setting isEnabled on the screen. We will start observing the transformed signal via observe primitive.
let observer =
Signal<Bool, NoError>.Observer(value: { value in
button.isEnabled = value
})
let disposable = transformedSignal.observe(observer)
Step 4: Stop observing a signal
Observe primitive returns a instance which can be used to stop signal observing
disposable.dispose()
Conclusion
This is how the different basic building blocks or primitives are used to write code in a functional reactive way. Winklix is iPhone app development company who create app that get’s featured in App Store . In case you are willing to have your app developed by us , you can contact us .