TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Runtime error after adding CloudKit to SwiftData App

Forums > SwiftUI

After converting an app to use SwiftData I added iCloud capability. The App runs fine after fixing some vars with no defaults. However, one View crashes with an error I cannot figure out (myRenderer.render in code below) The console shows:

error: Store failed to load. <NSPersistentStoreDescription: 0x600000d1d890> (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."; }

I saw a reference to a similar issue that seemed tied to iOS 17.4 (https://stackoverflow.com/questions/78123311/swiftdata-crash-on-launch-in-ios-17-4-the-configuration-named-default-does-no) Their solution did not seem to relate to my situation as it was an error on loading.

Any suggestions?

import SwiftUI
import SwiftData

struct RegattaResults: View {
    @Environment(\.modelContext) private var modelContext
    @Environment(\.dismiss) var dismiss
    var regatta : Regatta

    var theRaces : [Race] {
        (regatta.races ?? []).sorted(by: {$0.raceNum < $1.raceNum})
    }

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

            }
            Spacer()
            Text(regatta.name).font(.title2)
            Spacer()
            ResultsView(regatta: regatta)
        }
        .onAppear {
            regatta.processScores()
        }
    }

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

        // 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                                        //Thread 1 Fatal error: 'try!' expression unexpectedly raised an error: SwiftData.SwiftDataError(_error: SwiftData.SwiftDataError._Error.loadIssueModelContainer)

            // 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
    }

   

Note: This works on iOS 17.2 I have submitted feedback

   

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.