Explaining Combine's Share and Connect with Examples

Apple’s Combine framework has two operators called .share() and .makeConnectable(). They are extremely useful but often misunderstood. Both help us avoid unnecessary processing. .share(), as the name suggests, helps us share a subscription. .makeConnectable() helps control when publishers emit events. Working with Multiple Subscribers Imagine you have a simple view with a button and two labels. When a user taps the button, the text on the two labels will be updated. ...

January 29, 2023 · Mike Gopsill ·  Combine

Swift Combine's Future and Deferred Explained

Future Combine provides the Future publisher class to model a single asynchronous event. The Future acts like a promise. It can either fulfil its promise or fail to fulfil its promise. Those familiar with promises from frameworks like PromiseKit might liken Future to a promise. The added benefit of using Future is that it is a publisher, so you can use them with operators and subscribers and easily integrate them into combine data streams. ...

January 25, 2023 · Mike Gopsill ·  Combine

Swift Combine Publishers: An Overview

In this post, we will look at the Publisher protocol in Swift’s Combine framework. What is it? Why is it? 🤔 Why does Publisher exist? Combine is swift’s reactive programming framework. A reactive programming framework works with streams of data emitted over time. Combine needs something that can model a data stream. This is the role of the Publisher protocol. The protocol looks like this: protocol Publisher<Output, Failure> For the protocol to model a data stream it needs to allow for three things: ...

January 21, 2023 · Mike Gopsill ·  Combine

Creating a Text Publisher from UISearchBar in Combine

So you have a UISearchBar like the one shown below. A user typing into a search bar is an ideal input data stream for Combine. Ideally, we’d access a string publisher based on user input like so. let searchBar = UISearchBar() let searchText = searchBar.textPublisher Alas, it does not exist. I also tried performing Key-Value Observing with Combine on the searchBar or its text field. searchBar.publisher(for: \.text) searchBar.searchTextField.publisher(for: \.text) It compiles but emits no events. ...

Reactive Programming with Swift and Combine

Data Streams Reactive programming is programming with asynchronous data streams This is useful because anything can be represented as a stream of data, including user input, network responses, and even internal program state. By treating everything as a stream, it becomes possible to use a single set of abstractions to handle all types of data, making it simpler to reason about and manage the flow of data through an application. ...

January 15, 2023 · Mike Gopsill ·  Combine

Simulating Push Notifications on the iOS Simulator

Testing push notifications can be a challenging task, especially if you don’t have a physical device on hand to test with. Fortunately, there are tools available that allow you to test push notifications on the iOS simulator. Let’s explore how to use the simctl command line tool to simulate push notifications on the iOS simulator. Prerequisites Before we get started, you’ll need to make sure you have the following: A Mac with Xcode and the iOS simulator installed An app installed on the iOS simulator that you want to send push notifications to Grant Permissions Before you can schedule or deliver notifications, you need to ask the user for permission to do so. ...

January 6, 2023 · Mike Gopsill ·  simctl