Core data: Failed to load model

僤鯓⒐⒋嵵緔 提交于 2020-01-10 09:22:59

问题


I am new to core data.

What I am trying to DO: I am trying to create a cocoatouch framework that has an app to add employee details and display them in a table view. So that i can add this framework to my main project to work independently.

Issues I face: The frame work builds without any error. I have added the core data stack from swift 3 to the framework. But when i run the main project, the moment the framework loads the log displays "Failed to load model named Simple framework", "fetch failed" and "employee must have a valid entity description". The code that I have used in the framework is as shown below :

public class CoreDataStack {
    public static let sharedInstance = CoreDataStack()

    lazy var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentContainer(name: "SimpleFramework")
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error {
                fatalError("Unresolved error \(error), \(error)")
            }
        })
        return container
    }()

    public func saveContext() {
        let context = persistentContainer.viewContext
        if context.hasChanges {
            do {
                try context.save()
            } catch let error as NSError {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        }
    }
}

@IBAction func addEmployee(_ sender: Any) {

    //To save the data
    let context = CoreDataStack.sharedInstance.persistentContainer.viewContext
    let employee = Employee(context: context)
    employee.employeeName = nameTextField.text
    employee.employeeAge = Int16(ageTextField.text!)!
    employee.hasVehicle = hasVehicle.isOn
    CoreDataStack.sharedInstance.saveContext()
    navigationController!.popViewController(animated: true)
}

@IBAction func addEmployee(_ sender: Any) {

    //To save the data
    let context = CoreDataStack.sharedInstance.persistentContainer.viewContext
    let employee = Employee(context: context)
    employee.employeeName = nameTextField.text
    employee.employeeAge = Int16(ageTextField.text!)!
    employee.hasVehicle = hasVehicle.isOn
    CoreDataStack.sharedInstance.saveContext()
    navigationController!.popViewController(animated: true)
}


回答1:


I've had this issue, when I had wrong model name - it should me models name, not the projects (see the screen shot)




回答2:


Explicitly pass the models file name to the Core Data stack for initialization and make sure, it is loaded from the right bundle at the time (test bundle, app bundle...) by using Bundle(for: type(of: self)):

//...
let momdName = "SimpleFramework" //pass this as a parameter
//...

guard let modelURL = Bundle(for: type(of: self)).url(forResource: momdName, withExtension:"momd") else {
        fatalError("Error loading model from bundle")
}

guard let mom = NSManagedObjectModel(contentsOf: modelURL) else {
    fatalError("Error initializing mom from: \(modelURL)")
}

persistentContainer = NSPersistentContainer(name: momdName, managedObjectModel: mom)

//...

Edit:

Also make sure, the SimpleFramework.xcdatamodeld is added to the used targets Target Membership:




回答3:


The string you pass to the NSPersistentContainer initializer:

NSPersistentContainer(name: "CoreData")

needs to match the filename of the data model file in your Xcode project:

CoreData.xcdatamodeld



回答4:


In my case, for some reason the DataModel.xcdatamodeld became missing from my project workspace.

First I tried creating a new DataModle.xcdatamodeld and recreating the data model, but the same error occurred. Thats when I realized that the Original DataModel.xcdatamodeld was still in the root directory. I fixed this by simply right clicking my project in my project navigator, and selecting "Add files to "Project"...", then I added my old data model and deleted my new data model. Finally I hard cleaned, ran my project and it fixed the issue.



来源:https://stackoverflow.com/questions/42553749/core-data-failed-to-load-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!