Tirupati Balan

Tirupati Balan

Understanding Combine

Swift

Combine is one of those frameworks in Swift that might sound a little intimidating at first but holds incredible power once you get the hang of it. It provides a declarative Swift API for processing values over time. In simpler terms, it helps you manage complex data flows in your apps in a more readable and maintainable way.

Basic of Combine

Before diving into the technical details, let’s start with a simple analogy. Imagine you’re on a fishing trip. Your fishing rod is your “Subscriber,” the lake filled with fish is your “Publisher,” and each fish you catch is a “Value.” You are the “Observer,” excitedly watching your fishing rod to see what you’ll catch next.

What are Publishers and their Usage?

Publishers in Combine are like the lake filled with fish. They’re responsible for emitting a sequence of values over time. Whenever there’s a new value available, the publisher will notify all of its subscribers.

import Combine

let myPublisher = [1, 2, 3, 4].publisher

What is Subscription and its Usage?

Continuing with our fishing analogy, the subscription is the act of casting your line into the lake. A subscription connects a Publisher to a Subscriber, establishing a channel where data can flow.

let mySubscription = myPublisher.sink { value in
    print("Received value: \(value)")
}

What is Sink?

Sink is a subscriber that “sinks” all the values it receives, performing an action with them. It’s like a fishing net that catches every fish that swims into it. With sink, you define a closure to run whenever a new value is emitted.

myPublisher.sink { value in
    print("Received value: \(value)")
}

Other Chains of Operations: Filters and Custom Publishers

Sometimes you want to perform transformations on the values you get, like sorting or filtering the fish by size. In Combine, you can do this using operations like filter, map, merge, etc.

let filteredSubscription = myPublisher
    .filter { $0 > 2 }
    .sink { value in
        print("Filtered value: \(value)")
    }

What is Cancellable and Why is it Important?

Cancellable is like a fishing license. It signifies the agreement between the publisher and the subscriber. When you no longer want to receive updates, you can simply cancel your subscription.

mySubscription.cancel()

Creating Your Own Publisher and Controlling Access

Using PassThroughSubject

If you want to create your own lake (Publisher), you can use something like PassThroughSubject.

let subject = PassthroughSubject<Int, Never>()

Using AnyPublisher

To stop someone from accessing your lake directly (i.e., restrict them from sending their own fish), you can expose it as an AnyPublisher.

let publicPublisher: AnyPublisher<Int, Never> = subject.eraseToAnyPublisher()

Using @Published

Instead of using PassThroughSubject, you can use @Published for simpler tasks like notifying changes in a property value.

class MyClass {
    @Published var myProperty: Int = 0
}

Important Things to Remember in Combine

  • Publisher: The source of data, like a lake full of fish.
  • Observer of Publisher: That’s you, watching your fishing rod.
  • Subject: A special kind of publisher that you can inject values into.
  • Operation: Various ways to filter or manipulate the incoming data.
  • Cancellable: The agreement that lets you stop receiving updates when you no longer need them.

Combine is an incredibly powerful tool for managing data flow in Swift applications. From basic publishers and subscribers to advanced custom publishers and data manipulations, Combine provides a range of functionalities to make your coding life easier. So, the next time you find yourself drowning in a sea of asynchronous data, remember Combine could be your lifesaver!