Revealed on: August 12, 2024
When you begin migrating to the Swift 6 language mode, you may most definitely activate strict concurrency first. As soon as you have accomplished this there shall be a number of warings and errors that you’re going to encounter and these errors could 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 whenever you wish to undertake the Swift 6 language mode. Just about all the warnings you may get in strict concurrency mode will let you know about potential points associated to working 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 e book with a single buy.
WIth that out of the best way, let’s check out the next warning that you just would possibly encounter in your mission:
Changing non-sendable operate worth could introduce knowledge races
Normally the warning is a little more detailed, for instance in a mission I labored on this was the complete warning:
Changing non-sendable operate worth to ‘@Sendable (Information?, URLResponse?, (any Error)?) -> Void’ could introduce knowledge races
This warning (or error within the Swift 6 language mode) tells you that you just’re making an attempt to move a non-sendable closure or operate to a spot that expects one thing that is @Sendable
. For comfort I’ll solely use the time period closure however this is applicable equally to features.
Take into account a operate that is outlined as follows:
func performNetworkCall(_ completion: @escaping @Sendable (Information?, URLResponse?, (any Error)?) -> Void) {
// ...
}
This operate must be known as with a closure that is @Sendable
to be sure that we’re not introducting knowledge races in our code. Once we try to name this operate with a closure that is not @Sendable
the compiler will complain:
var notSendable: (Information?, URLResponse?, (any Error?)) -> Void = { knowledge, response, error in
// ...
}
// Changing non-sendable operate worth to '@Sendable (Information?, URLResponse?, (any Error)?) -> Void' could introduce knowledge races
performNetworkCall(notSendable)
The compiler is unable to ensure that our closure is protected to be known as in a unique actor, process, or different isolation context. So it tells us that we have to repair this.
Normally, the repair for this error is to mark your operate or closure as @Sendable
:
var notSendable: @Sendable (Information?, URLResponse?, (any Error?)) -> Void = { knowledge, response, error in
// ...
}
Now the compiler is aware of that we intend on our closure to be Sendable
and it’ll carry out checks to be sure that it’s. We’re now additionally allowed to move this closure to the performNetworkCall
technique that you just noticed earlier.
If you would like to study extra about Sendable
and @Sendable
try my course or learn a abstract of the subject proper right here.