If you allow strict concurrency checks to your present tasks, it’s doubtless that Xcode will current a great deal of warnings and/or errors once you compile your undertaking for the primary time. On this submit, I’d like to try a particular sort of error that pertains to code that you just didn’t write.
The @preconcurrency
declaration may be added to:
- features
- sorts
- protocols
- imports
Let’s check out all of those areas to completely perceive how @preconcurrency
helps us allow strict concurrency checks even when we will’t replace all of our dependencies simply but.
@preconcurrency imports
To be particular, Xcode will typically supply a message that reads quite a bit like this:
Add
@preconcurrency
to suppressSendable
-related warnings from moduleMyModule
This error tells us that we’re importing a module that doesn’t seem to fully adhere to fashionable concurrency guidelines simply but. Since this may not be a module that you just personal, Xcode gives you the power to silence strict concurrency warnings and errors coming from this module.
You are able to do this by including @preconcurrency
to your import
assertion:
@preconcurrency import MyModule
By doing this, Xcode will know that any warnings associated to sorts coming from MyModule
must be suppressed.
If MyModule
shouldn’t be a module that you just personal, it makes loads of sense to suppress warnings; you possibly can’t repair them anyway.
Notice that this received’t suppress warnings associated to code from MyModule
that is Sendable
or up-to-date with fashionable concurrency. So for those who see warnings associated to concurrency on a module that you just’ve marked with @preconurrency
, you’ll need to repair these warnings as a result of they’re right.
Including @preconcurrency to sorts, features, and extra
Alternatively, you is perhaps engaged on a module that has adopted Swift Concurrency and also you’ve fastened your warnings. If that’s the case, you would possibly need to add @preconcurrency
to a few of your declarations to make sure that code that depends upon your module doesn’t break.
Adopting Swift Concurrency will imply that your module’s ABI modifications and that some older code may not be capable of use your modules if that older code doesn’t additionally undertake Swift Concurrency.
If that is the state of affairs you’re in, you might need up to date a few of your code from this:
public class CatalogViewModel {
public personal(set) var books: [Book] = []
public init() {}
func loadBooks() {
// load books
}
}
To this:
@MainActor
public ultimate class CatalogViewModel {
public personal(set) var books: [Book] = []
public init() {}
public func loadBooks() {
// load books
}
}
If in case you have pre-concurrency code that makes use of this class, it would look a bit like this:
class TestClass {
func run() {
let obj = CatalogViewModel()
obj.loadBooks()
}
}
Sadly including @MainActor
to our class within the module makes it in order that we will’t use our view mannequin except we dispatch to the primary actor ourselves. The compiler will present an error that appears a bit like this:
Name to major actor-isolated initializer ‘init()’ in a synchronous nonisolated context.
Name to major actor-isolated occasion methodology ‘loadBooks()’ in a synchronous nonisolated context.
This tells us that as a way to work together with CatalogViewModel
, we’ll have to replace our undertaking to make use of the primary actor. It will usually snowball into increasingly more code updates which makes the modifications in our module severely breaking.
We will apply @preconcurrency
to our view mannequin to permit code that hasn’t been up to date to work together with our view mannequin as if it was by no means major actor annotated:
@preconcurrency @MainActor
public ultimate class CatalogViewModel {
public personal(set) var books: [Book] = []
public init() {}
public func loadBooks() {
// load books
}
}
Notice that the above solely works for tasks that don’t allow strict concurrency checking
With the @preconcurrency
annotation in place for our complete class, the compiler will strip the @MainActor
annotation for tasks which have their concurrency checking set to minimal
. In case you’re utilizing strict concurrency checks, the compiler will nonetheless emit errors for not utilizing CatalogViewModel
with the primary actor.
In Abstract
With @preconcurrency
, we will import previous modules into new code and we will permit utilization of recent code in previous tasks. It’s a good way to begin to incrementally undertake strict concurrency as the discharge of Swift 6 comes nearer and nearer.
Including @preconcurrency
to your imports may be very helpful once you’re importing modules that haven’t but been up to date for strict concurrency.
Including @preconcurrency
to declarations that you just’ve annotated as @Sendable
, @MainActor
, or in any other case up to date in a means that makes it not possible to make use of them in non-concurrent code could make loads of sense for library authors.