Sequence Operators in Combine Framework

Nilaakash Singh
Dev Genius
Published in
3 min readFeb 21, 2022

--

In this article we are going to talk about sequence operators of Combine Framework in iOS, that can make your life easy while manipulating data.

Use Cases: Different types of sequence operators offered by Combine Framework can be used at a very early stage of data subscription, it can avoid extra calculations for us after getting data.

Prerequisites:

  • Xcode 11 and above which supports Combine framework
  • Basic knowledge of Publishers, Subscribers and Subjects.

Let's get started:

Min and Max:

As the name suggests, min and max operators are used to get min and max values from the data types. Let's see an example below:

Min and Max Sequence Operators in Combine Framework.

In the above example, We have an array of int which is a publisher, when we subscribe it on Line 4 we are using .min() operator which will give us a result as -90. Similarly for .max() we get output as 200.

Reduce:

Reduce can help us to get results from operations such as addition, multiplication, subtraction and deletion of elements in the range, array, data structures etc.

Reduce Sequence Operator in Combine Framework.

In the above example we have made an array of random size and published it on Line 4.

In Line 5, we are using reduce() operator with initial value as “0". It contains 2 attributes:

  • Partial result: It stores operation result after every loop.
  • Value: Contains element value on current index of loop.

Loop is nothing but traversing instance on specific index of operation.

Count:

Count can help us get count of published elements. There are two ways we can use count:

  • Publishing all elements at once and then checking count.
  • Publishing one element at a time and getting counted after finishing the subscription.
Count Sequence Operator in Combine Framework.

In the above example, we can see count is used in both ways. In Line 6 we can use count when we have static elements.

Starting from Line 9 we have made passthrough subject and on line 10, We have used count operator. If you see the comment on Line 14 we will have to finish the subscription to get the total count.

Contains:

Contains operator works same as swift. It uses === (pointer comparison) for objects and “==” when it confirms to equatable. We can use it way earlier till we get subscription. We can avoid extra computation while checking different conditions as per logic using this operator.

Contains Sequence Operator in Combine Framework.

The implementation of this variable is very simple and self explanatory. Line 4 and Line 6 checks for two strings and result differs as per their availability in published variable.

AllSatisfy:

AllSatisfy is one of my favourite operator in combine sequence operator where we can check a pattern at a very early stage of subscription, this can avoid so many extra calculations at later stage.

AllSatisfy Sequence Operator in Combine Framework.

In the above example we’ve made a publisher and we are checking a very basic pattern where different elements are divided by 2 and 3 and remainder should be 0.

Line 4 will give result as true whereas at Line 5 will the condition will fail. This is where we can discard the usage of above variable/pattern with any work divided by 3.

Output:

Output operator can be used to get elements at index , in range as per their usage. We can use this variable to get the only result required for our logic.

Output Sequence Operator in Combine Framework.

In the above example we have used both types of output operator which combine offers.

Line 3 gives us the output of element at index 3 which is 80. Line 4 gives us the output in range of 0…2, which is “10, 20, 70”.

Note: First and Last is also available in sequence operators, however I feel it is same as First and Last of Filtering operator of combine which would be my next article.

Where to go from here:

We can try using this sequence operators at a time when we are using subscribers to get data. You can find different combine operator and their usage in my GitHub repository.

Share this blog to spread the knowledge | Do-Follow, and CLAP if you Liked it! Happy Coding :)

Add me on Linkedin.

--

--

Senior iOS Engineer at Loblaw Digital | Swift | SwiftUI | Combine | Swift Async | Blogger.