Sunday, May 26, 2024
HomeiOS Developmentios - NSEntityDescription Error when making an attempt to supply modelContainer to...

ios – NSEntityDescription Error when making an attempt to supply modelContainer to WindowGroup


I am having a difficulty when making an attempt to supply my mannequin container to my WindowGroup. I get the error:

‘NSInternalInconsistencyException’, purpose: ‘NSFetchRequest couldn’t find an NSEntityDescription for entity title ‘TaskItem”

Unsure what the difficulty might be. Any perception can be significantly appreciated.

Here’s a simplified model of my code:

TaskItem.swift:

import SwiftUI
import SwiftData

@Mannequin
class TaskItem: Identifiable {
    var id: UUID
    var taskTitle: String
    var creationDate: Date
    var isCompleted: Bool
    var tint: String
    
    init(id: UUID = .init(), taskTitle: String, creationDate: Date = .init(), isCompleted: Bool = false, tint: String) {
        self.id = id
        self.taskTitle = taskTitle
        self.creationDate = creationDate
        self.isCompleted = isCompleted
        self.tint = tint
    }
    
    var tintColor: Colour {
        swap tint {
        case "TaskColor 1": return .taskColor1
        case "TaskColor 2": return .taskColor2
        case "TaskColor 3": return .taskColor3
        case "TaskColor 4": return .taskColor4
        case "TaskColor 5": return .taskColor5
        default: return .black
        }
    }
}

DIController.swift:

import Swinject
import Supabase

class DIContainer {
    static let shared = DIContainer()
    let container: Container

    non-public init() {
        container = Container()
    }

    func initializeDependencies() async {
        container.register(LoginViewModel.self) { resolver in
            LoginViewModel(supabaseClient: supabaseClient)
        }
    }
}

MyApp.swift

import SwiftUI
import Swinject
import SwiftData

@principal
struct MyApp: App {
    let persistenceController = PersistenceController.shared

    init() {
        DIContainer.shared.initializeDependencies()
    }

    var physique: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(DIContainer.shared.container.resolve(LoginViewModel.self)!)
                //.modelContainer(persistenceController.container)
                //.modelContainer(for: TaskItem.self)
        }
        //.modelContainer(for: TaskItem.self)
        .modelContainer(persistenceController.container)
    }
}

PersistenceController.swift

import SwiftData

class PersistenceController {
    static let shared = PersistenceController()
    let container: ModelContainer

    init() {
        // Initialize the ModelContainer with the TaskItem mannequin
        container = strive! ModelContainer(for: TaskItem.self)
    }
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments