Swift

Building a token refresh flow with async/await and Swift Concurrency

Published on: August 16, 2021

One of my favorite concurrency problems to solve is building concurrency-proof token refresh flows. Refreshing authentication tokens is something that a lot of us deal with regularly, and doing it correctly can be a pretty challenging task. Especially when you want to make sure you only issue a single token refresh request even if multiple network calls encounter the need to refresh a token. Furthermore, you want to make sure that you automatically retry a request that failed due to a token expiration after you've obtained a new (valid) authentication token. I wrote about a flow that does this before,...

Read more...

Using Swift Concurrency’s task group for tasks with varying output

Published on: August 9, 2021

Earlier, I published a post on Swift Concurrency's task groups. If you haven't read that post yet, and you're not familiar with task groups, I recommend that you read that post first because I won't be explaining task groups in this post. Instead, you will learn about a technique that you can use to work around a limitation of task groups. Task groups can run a number of child tasks where every child task in the task group produces the same output. This is a hard requirement of the withTaskGroup function. This means that task groups are not the right...

Read more...

How to use async let in Swift?

Published on: August 9, 2021

In last week's post, I demonstrated how you can use a task group in Swift to concurrently run multiple tasks that produce the same output. This is useful when you're loading a bunch of images, or in any other case where you have a potentially undefined number of tasks to run, as long as you (somehow) make sure that every task in your group produces the same output. Unfortunately, this isn't always a reasonable thing to do. For example, you might already know that you only have a very limited, predetermined, number of tasks that you want to run. These...

Read more...

Swift Concurrency’s TaskGroup explained

Published on: August 5, 2021

With Apple's overhaul of how concurrency will work in Swift 5.5 and newer, we need to learn a lot of things from scratch. While we might have used DispatchQueue.async or other mechanisms to kick off multiple asynchronous tasks in the past, we shouldn't use these older concurrency tools in Swift's new concurrency model. Luckily, Swift Concurrency comes with many features already which means that for a lot of our old uses cases, a new paradigm exists. In this post, you will learn what Swift Concurrency's task groups are, and how you can use them to concurrently perform a lot of...

Read more...

Using UISheetPresentationController in SwiftUI 3

Published on: June 30, 2021

This post applies to the version of SwiftUI that shipped with iOS 15, also known as Swift 3. To learn how you can present a bottom sheet on iOS 16 and newer, take a look at this post. With iOS 15, Apple introduced the ability to easily implement a bottom sheet with UISheetPresentationController in UIKit. Unfortunately, Apple didn't extend this functionality to SwiftUI just yet (I'm hoping one of the iOS 15 betas adds this...) but luckily we can make use of UIHostingController and UIViewRepresentable to work around this limitation and use a bottom sheet on SwiftUI. In this post,...

Read more...

Presenting a bottom sheet in UIKit with UISheetPresentationController

Published on: June 30, 2021

We've seen bottom sheets in Apple's apps for a while now, and plenty of apps have followed this pattern. If you're not sure what I mean, it's the kind of sheet that takes up just a part of the screen and can be swiped upwards to take up the whole screen or downwards to be dismissed. Here's an example from Apple's Maps app: To implement a sheet like this, we used to require third party tools, or we needed to get creative and implement this pattern ourselves. With iOS 15, Apple introduced UISheetPresentationController which allows us to implement bottom sheets...

Read more...

What’s the difference between a singleton and a shared instance in Swift?

Published on: April 19, 2021

A common pattern on iOS, and in Swift, is to define an instance of an object that you can access from any place in your app. Common examples are URLSession.shared, FileManager.default, and UserDefaults.standard. These objects can all be considered shared instances, or globally available instances. Defining a shared instance is commonly done as follows: struct DataProvider { static let shared = DataProvider() // useful properties and methods } It's common for developers to call this a singleton, or a singleton instance. The singleton pattern is a pattern that allows developers to specify an object that can only ever have one...

Read more...

An introduction to JSON parsing in Swift

Published on: April 5, 2021

Virtually every modern application needs some way to retrieve, and use, data from a remote source. This data is commonly fetched by making a network request to a webserver that returns data in a JSON format. When you're working with Javascript, this JSON data can be easily decoded into a Javascript object. Javascript doesn't have strong typing, so a JSON object in Javascript is really just a JavaScript Object. Objects in Javascript are very comparable to dictionaries in Swift, except they aren't strongly typed and they have a couple of extra features. But that's way beyond what I want to...

Read more...

10 things iOS developers should focus on in 2021

Published on: January 4, 2021

I know. This is a clickbaity title. And yes, I know that this list is not relevant for everybody. I know that not every iOS developer has to learn everything on this list. That said, this list is a list of technologies and skills that I think are either already important, or becoming increasingly important this year. It's a list of technologies and skills that I have learned, plan to learn, or would like to learn this year. It's also a list that hopefully inspires you to broaden your horizons, and learn new things. Or maybe this list inspires you...

Read more...

Building a simple remote configuration loader for your apps

Published on: October 26, 2020

Remote configuration is a common practice in almost every app I have worked on. Sometimes these configurations can be large and the implications of a configuration change can be far-reaching while other times a configuration is used to change the number of items shown in a list, or to enable or disable certain features. You can even use remote configuration to set up your networking layer. For example by setting certain headers on a request, providing endpoints for your remote data, and more. In this week's post I will not go into detail about every possible use case that you...

Read more...