23.9 C
New York
Thursday, September 5, 2024

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


Printed on: August 12, 2024

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

I will 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 the entire 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 information races I extremely suggest that you simply check out my Swift Concurrency course which can get you entry to a collection of movies, workouts, and my Sensible Swift Concurrency ebook with a single buy.

WIth that out of the way in which, let’s check out the next warning that you simply may encounter in your undertaking:

Changing non-sendable perform worth might introduce information races

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

Changing non-sendable perform worth to ‘@Sendable (Information?, 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 go a non-sendable closure or perform 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 perform that is outlined as follows:

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

This perform ought to be known as with a closure that is @Sendable to ensure that we’re not introducting information races in our code. After we attempt to name this perform with a closure that is not @Sendable the compiler will complain:

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

// Changing non-sendable perform worth to '@Sendable (Information?, 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, job, or different isolation context. So it tells us that we have to repair this.

Often, the repair for this error is to mark your perform or closure as @Sendable:

var notSendable: @Sendable (Information?, 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 ensure that it’s. We’re now additionally allowed to go this closure to the performNetworkCall technique that you simply noticed earlier.

If you would like to be taught 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

Latest Articles