Understanding Task and Child Task in Swift's Structured Concurrency

In the realm of programming, the ability to handle concurrent tasks is vital to creating applications that are both performant and user-friendly. The introduction of Swift’s Structured Concurrency has revolutionised the way developers handle async operations. This post will delve into the intricacies of tasks and child tasks in Swift’s Structured Concurrency and provide a comprehensive understanding of their uses. The Power of Tasks In Swift’s Structured Concurrency model, tasks are the building blocks of asynchronous operations. Marked by the async keyword, these tasks denote computations that are performed asynchronously. ...

Introduction to Structured Concurrency in Swift

Structured concurrency is a programming paradigm aimed at improving multithreaded code’s clarity, efficiency, and robustness. It’s a fresh approach that allows you to manage concurrent tasks in a predictable, logical structure. Hence, “structured” concurrency. What is Structured Concurrency? Structured concurrency fundamentally means that the lifetime of concurrent tasks is bound and managed. In essence, when a block of code starts a concurrent task, that task is ensured to finish before the block of code concludes. This intrinsic tying of task lifetimes leads to code that is easier to reason about, more predictable, and less prone to bugs. ...

Xcconfig Explained

Xcode project files can get messy as projects grow in size and complexity. The complexity is increased when there are multiple environments or configurations, each with unique settings. Xcconfig files are a way to manage this complexity. Let’s dive into understanding how to use Xcconfig files in Xcode projects. What is an Xcconfig file? An Xcconfig, or Xcode configuration file, is a plain text file used by Xcode to externalise configuration settings for build systems. These files use the .xcconfig file extension and can contain build settings, variable declarations, comments, etc. ...

May 15, 2023 · Mike Gopsill ·  Tools

Target Dependencies vs Link Binary with Libraries

When developing iOS applications with Xcode, it’s essential to understand the difference between “Target Dependencies” and “Link Binary with Libraries.” These two options found in the Build Phases settings of a target play vital roles in defining how your app interacts with external code. In this article, we’ll discuss what these options do and how they are different. Understanding Targets Before we delve into the differences, let’s first understand what a ‘Target’ is in Xcode. A target specifies a set of build configurations which define how to build a product in Xcode. It could be an app, a framework, or a unit test bundle. Each target contains information about how to build the product, including build settings, build phases, and the product’s dependencies. ...

May 12, 2023 · Mike Gopsill ·  Tools

Codespaces with a Swift Package

In this post we will learn how to build a Swift Package using a GitHub Codespace. I did this recently as an experiment. It was easier than expected. The codespace was quick to build and test on too. It could be a useful tool when you have a poor local environment, e.g. an aging Intel MacBook. That said, there’s limitations too. What are Codespaces Github codespaces are development environments in the cloud. They allow you to develop from your browser. Opening a codespace displays a VS Code IDE. You can install libraries, dependencies and tools. You can run any code you write in the terminal. ...

February 16, 2023 · Mike Gopsill ·  Tools

Working with Subjects in Combine

In this blog post, we explore the concept of subjects in the Combine framework. We’ll look at two types provided by Apple: CurrentValueSubject and PassthroughSubject. We’ll explore their differences and when to use them. What are Subjects? Subject is a type of publisher in the Combine framework that adheres to the Subject protocol. It has a .send(_:) method that allows developers to send specific values to a subscriber. Subjects put values into a stream. This is useful when you need to mix imperative code with Combine. ...

Programmatically Navigating a UIPageViewController using a UISegmentedControl

Let’s learn how to navigate a UIPageViewController using a UISegmentedControl in iOS. The UIPageViewController provides a way to display pages of content, where each page is managed by its own view controller. The UISegmentedControl displays segments and allows users to switch between them. We’ll begin by setting up the UISegmentedControl and UIPageViewController, then move on to connecting them by programmatically navigating the UIPageViewController based on the selection in the UISegmentedControl. ...

Using URLProtocol to Mock and Test

When writing unit tests for Swift networking code that uses URLSession we don’t want to perform real network requests. We want to mock responses so that our unit tests remain fast, predictable and independent. Mocking responses from URLSession can be done by wrapping it in a protocol and creating a mock session when testing. But an alternative way is using URLProtocol. What is URLProtocol? URLProtocol is a class that allows you to intercept and handle network requests, giving you the ability to implement features such as caching, redirects, or authentication. When it comes to testing, it gives the ability to intercept requests and return mocked responses, as well as spy on the request that was sent. ...

February 1, 2023 · Mike Gopsill ·  Testing

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