@preconcurrency utilization in swift defined – Donny Wals


Whenever you allow strict concurrency checks on your present initiatives, it’s doubtless that Xcode will current a great deal of warnings and/or errors while you compile your challenge for the primary time. On this publish, I’d like to check out a particular type of error that pertains to code that you just didn’t write.

The @preconcurrency declaration will be added to:

  • capabilities
  • varieties
  • protocols
  • imports

Let’s check out all of those areas to totally perceive how @preconcurrency helps us allow strict concurrency checks even when we are able to’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 suppress Sendable-related warnings from module MyModule

This error tells us that we’re importing a module that doesn’t seem to utterly adhere to fashionable concurrency guidelines simply but. Since this may not be a module that you just personal, Xcode affords you the flexibility 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 varieties coming from MyModule ought to be suppressed.

If MyModule isn’t a module that you just personal, it makes numerous sense to suppress warnings; you possibly can’t repair them anyway.

Be aware that this gained’t suppress warnings associated to code from MyModule that is Sendable or up-to-date with fashionable concurrency. So in the event you see warnings associated to concurrency on a module that you just’ve marked with @preconurrency, you’ll wish to repair these warnings as a result of they’re right.

Including @preconcurrency to varieties, capabilities, and extra

Alternatively, you may be engaged on a module that has adopted Swift Concurrency and also you’ve fastened your warnings. If that’s the case, you may wish 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 adjustments and that some older code may not be capable to 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 remaining class CatalogViewModel {
  public personal(set) var books: [Book] = []

  public init() {}

  public func loadBooks() {
    // load books
  }
}

In case you have pre-concurrency code that makes use of this class, it’d 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 are able to’t use our view mannequin until we dispatch to the primary actor ourselves. The compiler will present an error that appears a bit like this:

Name to fundamental actor-isolated initializer ‘init()’ in a synchronous nonisolated context.
Name to fundamental actor-isolated occasion methodology ‘loadBooks()’ in a synchronous nonisolated context.

This tells us that so as to work together with CatalogViewModel, we’ll have to replace our challenge to make use of the primary actor. This can typically snowball into an increasing number of code updates which makes the adjustments 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 fundamental actor annotated:

@preconcurrency @MainActor 
public remaining class CatalogViewModel {
  public personal(set) var books: [Book] = []

  public init() {}

  public func loadBooks() {
    // load books
  }
}

Be aware that the above solely works for initiatives 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 initiatives which have their concurrency checking set to minimal. Should 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 are able to import outdated modules into new code and we are able to enable utilization of latest code in outdated initiatives. 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 while 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 method that makes it unimaginable to make use of them in non-concurrent code could make numerous sense for library authors.

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox