Printed on: July 31, 2024
When you begin migrating to the Swift 6 language mode, you will most certainly activate strict concurrency first. As soon as you’ve got achieved this there shall be a number of warings and errors that you’re going to encounter and these errors may be complicated at occasions.
I am going to begin by saying that having a stable understanding of actors, sendable, and knowledge races is a large benefit while you wish to undertake the Swift 6 language mode. Just about all the warnings you will get in strict concurrency mode will inform you about potential points associated to operating code concurrently. For an in-depth understanding of actors, sendability and knowledge races I extremely suggest that you just check out my Swift Concurrency course which can get you entry to a sequence of movies, workouts, and my Sensible Swift Concurrency guide with a single buy.
WIth that out of the way in which, let’s check out the next warning that you just may encounter in your undertaking:
Reference to captured var in concurrently-executing code
This warning tells you that you just’re capturing a variable inside a physique of code that can run asynchornously. For instance, the next code will outcome on this warning:
var process = NetworkTask(
urlsessionTask: urlSessionTask
)
add(fromTask: urlSessionTask, metaData: metaData, completion: { end in
Job {
await process.sendResult(outcome) // Reference to captured var 'process' in concurrently-executing code; that is an error within the Swift 6 language mode
}
})
The process
variable that we create a few traces earlier is mutable. Which means that we will assign a distinct worth to that process at any time and that would end in inconsistencies in our knowledge. For instance, if we assign a brand new worth to the process
earlier than the closure begins operating, we would have captured the previous process
which might be sudden.
Since strict concurrency is supposed to assist us be sure that our code runs as freed from surprises as potential, Swift desires us to be sure that we seize a relentless worth as an alternative. On this case, I am not mutating process
anyway so it is secure to make it a let
:
let process = NetworkTask(
urlsessionTask: urlSessionTask
)
add(fromTask: urlSessionTask, metaData: metaData, completion: { end in
Job {
await process.sendResult(outcome)
}
})
This transformation removes the warning as a result of the compiler now is aware of for positive that process
will not be given a brand new worth at some sudden time.
One other technique to repair this error can be to make in express seize within the completion
closure that I am passing. This seize will occur instantly as a let
so Swift will know that the captured worth won’t change unexpectedly.
var process = NetworkTask(
urlsessionTask: urlSessionTask
)
add(fromTask: urlSessionTask, metaData: metaData, completion: { [task] end in
Job {
await process.sendResult(outcome.mapError({ $0 as any Error }))
}
})
Altenatively, you may make an express fixed seize earlier than your Job
runs:
var process = NetworkTask(
urlsessionTask: urlSessionTask
)
let theTask = process
add(fromTask: urlSessionTask, metaData: metaData, completion: { end in
Job {
await theTask.sendResult(outcome)
}
})
This isn’t as elegant however may be wanted in circumstances the place you do wish to go your variable to a chunk of concurrently executing code however you additionally need it to be a mutable property for different objects. It is basically the very same factor as making a seize in your completion closure (or instantly within the process if there is not any further wrapping closures concerned).
Once you first encounter this warning it may be instantly apparent why you are seeing this error and the way you must repair it. In digital all circumstances it implies that that you must both change your var
to a let
or that that you must carry out an express seize of your variable both by making a shadowing let
or via a seize record on the primary concurrent little bit of code that accesses your variable. Within the case of the instance on this publish that is the completion
closure however for you it may be instantly on the Job
.