Thread 1: "NSFetchRequest could not locate an NSEntityDescription for entity name 'Goal'"

Hello, how do I fix "Thread 1: "NSFetchRequest could not locate an NSEntityDescription for entity name 'Goal'"

import SwiftUI
import SwiftData

@main
struct Main: App {
    @Environment(\.scenePhase) private var scenePhase
    
    @StateObject var statsViewModel = StatsViewModel()
    @StateObject var badgeViewModel = BadgeViewModel()
    @StateObject var localNotificationManager = LocalNotificationManager()
    
    @AppStorage("notifications") var notificationsSet: Bool = false
    
    @Environment(\.modelContext) private var modelContext

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(statsViewModel)
                .environmentObject(badgeViewModel)
                .environmentObject(localNotificationManager)
                .task {
                    if !notificationsSet {
                        do{
                            try await localNotificationManager.reqeustAuthorization()
                            await localNotificationManager.setUpNotificationsAtStart()
                        } catch{
                            
                        }
                    }
                    notificationsSet = true
                }
        }
        .modelContainer(container)
        .onChange(of: scenePhase) { _, phase in
            if phase == .active{
                Task{
                    await localNotificationManager.getCurrentSettings()
                    await localNotificationManager.getPendingRequest()
                }
            } else if phase == .background{
                do {
                    try modelContext.save()
                } catch  {
                    print("COULD NOT SAVE WITH SWIFTDATA")
                }
            }
        }
    }
}

and

@MainActor
var container: ModelContainer = {
    let fileContainer = URL.storeURL(for: "group.Water-Alert-App", databaseName: "CoreData")
    
    let defaultDirectoryURL = NSPersistentContainer.defaultDirectoryURL()
    
    let localSchema = Schema([Reminder.self])
    let cloudSchema = Schema([Goal.self, WaterData.self, Container.self])
    
    let localConfiguration = ModelConfiguration("Local", schema: localSchema, url: defaultDirectoryURL.appendingPathComponent("Local.sqlite"))
    
    let cloudConfiguration = ModelConfiguration("Cloud", schema: cloudSchema, url: fileContainer, cloudKitDatabase: .private("iCloud.Water-Alert-App-Offical"))
    
    let container = try! ModelContainer(for: Reminder.self, Goal.self, WaterData.self, Container.self, configurations: localConfiguration, cloudConfiguration)
    
    return container
}()

this is the code that makes the app crash

Thank You

Answered by Jad-T in 779031022

From Apple DTS:

This is an issue on the framework side – when an app uses a ModelContainer with multiple ModelConfiguration to manage multiple stores, only the model types specified in the first configuration are loaded, and that triggers the error.

This issue should have been fixed in iOS 17.4, which is now beta 2. You can download the beta and give it a try. Feel free to follow up if the issue is still there.

If you would like to support versions before 17.4, consider creating one container per configuration.

Accepted Answer

From Apple DTS:

This is an issue on the framework side – when an app uses a ModelContainer with multiple ModelConfiguration to manage multiple stores, only the model types specified in the first configuration are loaded, and that triggers the error.

This issue should have been fixed in iOS 17.4, which is now beta 2. You can download the beta and give it a try. Feel free to follow up if the issue is still there.

If you would like to support versions before 17.4, consider creating one container per configuration.

Thread 1: "NSFetchRequest could not locate an NSEntityDescription for entity name 'Goal'"
 
 
Q