In Objective-c we can use NS_SWIFT_UI_ACTOR
annotation to indicate that a method or all methods on a class should be executed on the main actor (as we would do with @MainActor
in Swift).
This works correctly when invoking a method which returns a value (method1
in this example) from outside the main actor (using a Task.detached
): the compiler forces us to call it using "await" (to allow for "actor switching") and, at runtime, I can see that the code is executed on the main thread.
However, when calling a callback based method on the same class (which gets an automatically "imported" async variant, as explained here) the NS_SWIFT_UI_ACTOR
seems not working properly, and the code is executed on a background thread.
NS_SWIFT_UI_ACTOR
@interface ObjcClass : NSObject
- (NSString *)method1;
- (void)method2WithCompletion: (void (^)(NSString * _Nonnull))completion;
@end
Task.detached(priority: .background) {
let objcInstance = ObjcClass()
print(await objcInstance.method1()) <----- this is called on the main thread
print(await objcInstance.method2()) <----- this is called on a bg thread
}
Could this be related to the automatically "imported" async variant which isn't correctly handled or am I missing something?