79409890

Date: 2025-02-03 20:12:11
Score: 0.5
Natty:
Report link

You state that @MainActor is not an option here, without further explanation. So without understanding the rest of your project, I can suggest that it might be that class Foo should really be actor Foo in a fully compliant Swift Concurrency world.

The overall issue you are having seems to be that you are trying to pass around an instance of Foo on which you wish to call async methods, without supplying any guarantees to the compiler about that class' data race safety.

If it cannot or should not run as @MainActor, the same guarantee can be given either by making it an actor itself or by taking data safety entirely into your own hands and marking it as @unchecked Sendable, which will get the compiler off your back for better and for worse.

Either of these changes will make the code you have given here compile, however it's impossible to comment on the effect of the changes on the rest of your code.

actor Foo: NSObject {
    // maintains compiler-level data race safety enforcement
}

or...

class Foo: NSObject, @unchecked Sendable {
   // promises the compiler data race safety...
   // ...but removes its ability to enforce this
}
Reasons:
  • Blacklisted phrase (1): to comment
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: drseg