Contents

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.Snapshot

Parameters

  • source:

    The file URL to read from.

  • progress:

    A Subprogress value to report reading progress. Consume it once with reporter(totalCount:) and call complete(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
}

See Also

Reading a document