Updating an app to use strict concurrency
Use this code to follow along with a guide to migrating your code to take advantage of the full concurrency protection that the Swift 6 language mode provides.
Overview
This sample provides two separate versions of the app:
The original version uses Swift concurrency features but contains a number of issues that are detected by enabling Swift complete concurrency checking and that need to be resolved before enabling the Swift 6 language mode.
The updated version resolves these issues and has enabled Swift 6. It also adds new features that record the location of the user when they log that they drank coffee.
Watch the session to see the process step by step, and then compare the two projects to see the differences.
Configure the sample code project
To add the complication to an active watch face, start by building and running the sample code project in the simulator, and follow these steps:
Click the Digital Crown to exit the app and return to the watch face.
Using the trackpad, firmly press the watch face to put the face in edit mode, and then click Customize.
Swipe left until the configuration screen highlights the complications. Select the complication to modify.
Scroll to the complication for the sample app, and then click the Digital Crown again to save your changes.
Click the newly added complication to go to the app.
For more information on setting up watch faces, see Change the watch face on your Apple Watch.
After configuring and running the app, you can test the background updates. Make sure that the complication appears on the active watch face. Then build and run the app in Simulator, and follow these steps:
Add one or more drinks using the app’s main view.
Click the Digital Crown to send the app to the background.
Open Settings, and scroll down to Health > Health Data > Nutrition > Caffeine to see all of the drinks you added to the app.
Click Delete Caffeine Data to clear all of the caffeine samples from HealthKit.
Navigate back to the watch face.
The complication will update within 15 minutes; however, the update may be delayed based on the system’s current state.
Adopt strict concurrency checking in Swift
To see the issues highlighted in the original project, follow these steps:
In Xcode, navigate to the project settings.
For each target, change the Strict Concurrency Checking below Build Settings to Complete.
Each target generates warnings that must be resolved.
After the warnings are resolved, change the Swift Language Version in Build settings to Swift 6.
Compare the original project to the updated project to see how it resolves the concurrency issues.