Thursday, September 11, 2025
HomeiOS DevelopmentFixing “Changing non-sendable operate worth might introduce information races” in Swift

Fixing “Changing non-sendable operate worth might introduce information races” in Swift


Printed on: August 12, 2024

When you begin migrating to the Swift 6 language mode, you will most probably activate strict concurrency first. As soon as you’ve got performed this there can be a number of warings and errors that you will encounter and these errors will be complicated at occasions.

I am going to begin by saying that having a strong understanding of actors, sendable, and information races is a large benefit whenever you need 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 working code concurrently. For an in-depth understanding of actors, sendability and information races I extremely advocate that you simply check out my Swift Concurrency course which can get you entry to a sequence of movies, workout routines, 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 simply would possibly encounter in your undertaking:

Changing non-sendable operate worth might introduce information races

Normally the warning is a little more detailed, for instance in a undertaking I labored on this was the complete warning:

Changing non-sendable operate worth to ‘@Sendable (Knowledge?, URLResponse?, (any Error)?) -> Void’ might introduce information races

This warning (or error within the Swift 6 language mode) tells you that you simply’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 capabilities.

Take into account a operate that is outlined as follows:

func performNetworkCall(_ completion: @escaping @Sendable (Knowledge?, URLResponse?, (any Error)?) -> Void) {
    // ...
}

This operate ought to be known as with a closure that is @Sendable to make it possible for we’re not introducting information races in our code. Once we try to name this operate with a closure that is not @Sendable the compiler will complain:

var notSendable: (Knowledge?, URLResponse?, (any Error?)) -> Void = { information, response, error in 
    // ...
}

// Changing non-sendable operate worth to '@Sendable (Knowledge?, URLResponse?, (any Error)?) -> Void' might introduce information races
performNetworkCall(notSendable)

The compiler is unable to ensure that our closure is protected to be known as in a unique actor, activity, 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 (Knowledge?, URLResponse?, (any Error?)) -> Void = { information, 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 make it possible for it’s. We’re now additionally allowed to move this closure to the performNetworkCall technique that you simply noticed earlier.

If you would like to study extra about Sendable and @Sendable take a look at my course or learn a abstract of the subject proper right here.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments