OMGPOP needs to attract extra consideration to this query:
Please check out my pattern code. Thanks!
So I’ve this code:
fileprivate class DocumentScanDelegate: NSObject, VNDocumentCameraViewControllerDelegate {
static let shared = DocumentScanDelegate()
var compressionQuality: CGFloat = 1
var onScanSuccess: (UIImage) -> Void = { _ in }
func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
controller.dismiss(animated: true)
guard scan.pageCount >= 1 else { return }
let lastPage = scan.imageOfPage(at: scan.pageCount – 1)
let compressed = lastPage.compressed(high quality: compressionQuality)
onScanSuccess(compressed)
}
func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFailWithError error: Error) {
controller.dismiss(animated: true)
}
func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) {
controller.dismiss(animated: true)
}
}
I obtained 2 errors:
shared:
Static property ‘shared’ isn’t concurrency-safe as a result of non-‘Sendable’ sort ‘DocumentScanDelegate’ could have shared mutable state; that is an error within the Swift 6 language mode
the three controller.dismiss calls:
Name to major actor-isolated occasion methodology ‘dismiss(animated:completion:)’ in a synchronous nonisolated context; that is an error within the Swift 6 language mode
Each is smart. So what I did is including @MainActor to DocumentScanDelegate . Then first warning disappeared. However the second warning turns into:
Principal actor-isolated occasion methodology ‘documentCameraViewController(_:didFinishWith:)’ can’t be used to fulfill nonisolated protocol requirement; that is an error within the Swift 6 language mode
Then I take advantage of @preconcurrency to annotate VNDocumentCameraViewControllerDelegate conformance:
@MainActor
fileprivate class DocumentScanDelegate: NSObject, @preconcurrency VNDocumentCameraViewControllerDelegate {
…
}
This @preconcurrency trick is similar to WWDC 2024’s video (https://developer.apple.com/movies/play/wwdc2024/10169/?time=1520)
The error turns into:
Principal actor-isolated occasion methodology ‘documentCameraViewController(_:didFinishWith:)’ can’t be used to fulfill nonisolated protocol requirement; that is an error within the Swift 6 language mode
With the next sub-error:
Class ‘VNDocumentCameraScan’ doesn’t conform to the ‘Sendable’ protocol
Then I take advantage of @preconcurrency import:
@preconcurrency import VisionKit
The identical warning persists.
There’s additionally a brand new warning (why?):
‘@preconcurrency’ attribute on module ‘VisionKit’ has no impact