The Blog

Responding to changes in a managed object context

Working with multiple managed object contexts will often involve responding to changes that were made in one context to update another context. You might not even want to update another context but reload your UI or perform some other kind of update. Maybe you want to do this when a specific context updates, or maybe you want to run some code when any context updates. In this week’s post I will show you how you can listen for changed in Read more…

Building a concurrency-proof token refresh flow in Combine

Refreshing access tokens is a common task for many apps that use OAuth or other authentication mechanisms. No matter what your authentication mechanism is, your tokens will expire (eventually) and you’ll need to refresh them using a refresh token. Frameworks like RxSwift and Combine provide convenient ways to build pipelines that perform transformation after transformation on a succesful network response, allowing you to grab Data, manipulate and transform it to an instance of a model object or anything else. Programming Read more…

Building a simple remote configuration loader for your apps

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 Read more…

Formatting dates in the user’s locale using DateFormatter in Swift

Working with dates is hard, there is no doubt about that. And formatting dates properly for every user of your app is no easier (if you want to do everything manually). Luckily, the system can help us. For example, in the US one would write "October 15" while in The Netherlands we write 15 oktober. Note that the order of the date and the month is different, the spelling of the month is different and the capitalization is different too. Read more…

Observing changes to managed objects across contexts with Combine

A common pattern in Core Data is to fetch objects and show them in your UI using one managed object context, and then use another context to update, insert or delete managed objects. There are several ways for you to update your UI in response to these changes, for example by using an NSFetchedResultsController. I wrote about doing this in a SwiftUI app in an earlier post. In this week’s post I will show you a useful technique that allows Read more…

Understanding the differences between your Core Data model and managed objects

You may have noticed that when Xcode generates your NSManagedObject classes based on your Core Data model file, most of your managed object’s properties are optional. Even if you’ve made them required in the model editor, Xcode will generate a managed object where most properties are optional. In this article we’ll explore this phenomenon, and why it happens. Exploring generated NSManagedObject subclasses When you build a project that uses Xcode’s automatic code generation for Core Data models, your NSManagedObject subclasses Read more…

Understanding how DispatchQueue.sync can cause deadlocks

As a developer, you’ll come across the term "deadlock" sooner or later. When you do, it’s usually pretty clear that a deadlock is bad (the name alone implies this) and if you’ve experienced one in your code, you’ll know that your application crashes with EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) when a deadlock occurs. A few weeks ago, I wrote about dispatching code synchronously and asyncronously. Several people pointed out that that post does not mention deadlocks. Instead of making that post longer Read more…

Configuring error types when using flatMap in Combine

When you’re using Combine for an app that has iOS 13 as its minimum deployment target, you have likely run into problems when you tried to apply a flatMap to certain publishers in Xcode 12. To be specific, you have probably seen the following error message: flatMap(maxPublishers:_:) is only available in iOS 14.0 or newer. When I first encountered this error I was somewhat stumped. Surely we had flatMap in iOS 13 too! If you’ve encountered this error and thought Read more…

3 tips to work through a technical coding interview

If you’re a programmer looking for a job it’s very likely that you’ll have to do a coding interview at some point. Every company conducts these interviews differently. Some may have you work through several heavy computer science problems, others may present you with a task that’s related to the job you’re interviewing for and others might do both. No matter what the exact form is, you’ll want to nail these interviews as they are a big part of whether Read more…

Dispatching async or sync? The differences explained

When writing iOS apps, we regularly run into code that is asynchronous. Sometimes you know you’re writing something that will run asynchronously and other times you’re passing a completion handler to code that may or may not run asynchronously on a different dispatch queue. If you’re familiar with using DispatchQueue.main, you have probably written code like this: DispatchQueue.main.async { // do something } And while writing this, you may have encountered a second method on DispatchQueue.main called sync. In this Read more…