BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

Rendering to PDF fails in iOS 17.4 (OK in iOS 17.2)

Forums > SwiftUI

My results reporting view (SeriesResults below) worked fine until I selected a simulator or device running 17.4. I reported it in feedback 2 weeks ago, but have had no response. Perhaps someone can suggest a cause and/or a workaround?

It compiles OK, but as soon as this view tries to display (before the render is called), I get a fatal error: XCode flags the myRenderer.render line:

Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: SwiftData.SwiftDataError(_error: SwiftData.SwiftDataError._Error.loadIssueModelContainer)

Console Shows CoreData info:

CoreData: debug: CoreData+CloudKit: NSCloudKitMirroringDelegate managedObjectContextSaved(3113) <NSCloudKitMirroringDelegate: 0x600003d042d0>: Observed context save: <NSPersistentStoreCoordinator: 0x60000290d5e0> - <NSManagedObjectContext: 0x60000390e3c0> CoreData: error: Store failed to load. <NSPersistentStoreDescription: 0x600000d0a100> (type: SQLite, url: file :/// dev/null) with error = Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={NSLocalizedFailureReason=The configuration named 'default' does not contain any entities.} with userInfo { NSLocalizedFailureReason = "The configuration named 'default' does not contain any entities."; } _SwiftData_SwiftUI/ModelContainer+Extensions.swift:6: Fatal error: 'try!' expression unexpectedly raised an error: SwiftData.SwiftDataError(_error: SwiftData.SwiftDataError._Error.loadIssueModelContainer) error: Store failed to load. <NSPersistentStoreDescription: 0x600000d0a100> (type: SQLite, url: file :/// dev/ null) with error = Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={NSLocalizedFailureReason=The configuration named 'default' does not contain any entities.} with userInfo { NSLocalizedFailureReason = "The configuration named 'default' does not contain any entities."; } Unresolved error loading container Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={NSLocalizedFailureReason=The configuration named 'default' does not contain any entities.}

struct SeriesResults: View {
    @Environment(\.modelContext) private var modelContext
    @Environment(\.dismiss) var dismiss

    var series : Series

    var body: some View {
        Spacer()
        VStack {
            HStack {
                Text("  < Dismiss").onTapGesture {
                    dismiss()
                }.foregroundColor(.blue)
                Spacer()
                ShareLink("PDF", item: render())

            }
            Spacer()
            Text(series.seriesName).font(.title2)
            HStack {
                Spacer()
                Text(series.seriesScoring.rawValue).font(.subheadline)
                Spacer()
                Text("\(series.numThrows) Thrown Out")
                Spacer()
            }
            Spacer()
            SeriesResultsView(series: series)
        }
        .onAppear {
            series.updateResults()
        }
    }

    @MainActor func render() -> URL {
        // 1: Render Results with some modifiers
        let myRenderer = ImageRenderer(content: SeriesResultsViewForSharing(series: series)
        )

        // 2: Save it to our documents directory
        let url = URL.documentsDirectory.appending(path: "regattaScores.pdf")

        // 3: Start the rendering process
        myRenderer.render { size, context in
            // 4: Tell SwiftUI our PDF should be the same size as the views we're rendering
            var box = CGRect(x: 0, y: 0, width: size.width, height: size.height)

            // 5: Create the CGContext for our PDF pages
            guard let pdf = CGContext(url as CFURL, mediaBox: &box, nil) else {
                return
            }

            // 6: Start a new PDF page
            pdf.beginPDFPage(nil)

            // 7: Render the SwiftUI view data onto the page
            context(pdf)

            // 8: End the page and close the file
            pdf.endPDFPage()
            pdf.closePDF()
        }
        return url
    }
}

   

I was able to "work around" this by adding a modelContext modifier to the imageRenderer. If anyone can shed light on why this is needed (and why it was not in iOS 17.2 but is in 17.4!) I think I could learn something!

  @MainActor func render() -> URL {
        // 1: Render Results with some modifiers
        let myRenderer = ImageRenderer(content: ResultsViewForSharing(regatta: regatta)
            .modelContext(modelContext)
        )

1      

You have just completed days of research. I don't even know why I didn't come across your solution before, thank you very much!

   

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.