Contents

FileWrapperDocumentReader

A document reader that deserializes a FileWrapper into a snapshot.

Declaration

struct FileWrapperDocumentReader<Snapshot>

Overview

This is the recommended reader for most documents. Provide a closure that converts a FileWrapper into your snapshot type, and FileWrapperDocumentReader handles file coordination and loading.

func reader(configuration: sending ReadConfiguration) -> sending FileWrapperDocumentReader<String> {
    FileWrapperDocumentReader(configuration) { fileWrapper in
        guard let data =
            fileWrapper.regularFileContents else {
            throw CocoaError(.fileReadCorruptFile)
        }
        return String(decoding: data, as: UTF8.self)
    }
}

For package documents, navigate the FileWrapper hierarchy:

FileWrapperDocumentReader(configuration) { directory in
    let children = directory.fileWrappers ?? [:]
    guard let metadataData = children["metadata.json"]?
        .regularFileContents else {
        throw CocoaError(.fileReadCorruptFile)
    }
    return try JSONDecoder().decode(
        Metadata.self, from: metadataData
    )
}

The closure does not receive a Subprogress. To report progress during reads, use a custom DocumentReader instead.

Topics

Creating a reader

See Also

Reading and writing documents