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
}