How to resolve error:
Non-Sendable parameter type
BookInfoof actor-isolated @objc instance method cannot cross actor boundary?
BookStore.swift:
// manage state of instances of BookInfo
@objc actor BookStore: NSObject {
@objc static let shared = BookStore()
private var bookCache = [String: BookInfo]()
@objc func registerBook(bookInfo: BookInfo) async { //error: Non-Sendable parameter type `BookInfo` of actor-isolated @objc instance method cannot cross actor boundary
bookCache[bookInfo.id] = bookInfo
}
@objc func updateBookInfo(id: String) async {
// update properties in BookInfo
}
ClassA.m:
@implementation ClassA {
- (void)register() {
BookInfo someBookInfo = [[BookInfo alloc] initWithID:id author: author];
[[BookStore shared] registerBook:someBookInfo completionHandler^(){}];
// After rigistering book, it will not need read/write `someBookInfo` anymore
}
BookInfo.h:
@interface BookInfo : NSObject
@property (strong) Report *report;
@proptery (assign) NSString *id;
@property (Strong) Author: *author;
- (instancetype) initWithID:(NSString *)id author:(Author*)author;
// ...
//some method
@end
Discussion: Since the caller will not read/write someBookInfo after initialize it and pass in it as a param in registerBook, it is guaranteed that there will not be race condition on someBookInfo between the thread which runs the caller's method and the thread runs the calling method. Adding extension BookInfo: @unchecked Sendable {} will resolve the error at compiler time. I want to avoid that for certain risks that I'm not aware of. Any discussion/advice will be appreciated.