read(from:progress:)
Reads the document’s content from disk.
Declaration
@concurrent func read(from source: sending Self.Source, progress: consuming Subprogress) async throws -> sending Self.SnapshotParameters
- source:
The file URL to read from.
- progress:
A
Subprogressvalue to report reading progress. Consume it once withreporter(totalCount:)and callcomplete(count:)as units finish.
Return Value
A snapshot representing the document’s content.
Discussion
SwiftUI calls this method in the background with coordinated file access. Perform all deserialization and disk access here — the returned snapshot is delivered to apply(snapshot:previous:) on the main actor.
For most documents, use FileWrapperDocumentReader instead of implementing a custom reader. Only implement read yourself when you need capabilities FileWrapperDocumentReader doesn’t provide — such as direct URL access for Core Graphics, AVFoundation, or other frameworks that operate on file paths:
@concurrent
func read(from source: URL, progress: consuming Subprogress)
async throws -> sending CGImage {
guard let imageSource =
CGImageSourceCreateWithURL(source as CFURL, nil),
let image = CGImageSourceCreateImageAtIndex(
imageSource, 0, nil
) else {
throw CocoaError(.fileReadCorruptFile)
}
return image
}