After watching WWDC22 session Visualize and optimize Swift concurrency, I have a question regarding the following sample code:
actor ParallelCompressor {
// ......
unowned let status: CompressionState
// ......
nonisolated func compressFile(url: URL) async -> Data {
// ......
let compressedData = CompressionUtils.compressDataInFile(at: url) { uncompressedSize in
Task { @MainActor in
status.update(url: url, uncompressedSize: uncompressedSize)
}
} // ......
}
}
Here ParallelCompressor
stores CompressionState
as unowned let
, but later there is code using this member in an escaping context. For me, it is fairly possible status
could have been deinited when it is referenced in Task {}
, so there might be a runtime crash, am I right?
After some digging, it seems declaring status
as weak var
is not an option, as posted in this thread: Actor Isolation and weak vars.
So my question is, is there a better and safer way to express such scenario, where an actor must reference a non-retained object?