Core data: Failed to load model

2019-02-05 19:14发布

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

This is a screenshot of the console log.

4条回答
三岁会撩人
2楼-- · 2019-02-05 19:46

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楼-- · 2019-02-05 19:49

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

查看更多
萌系小妹纸
4楼-- · 2019-02-05 20:03

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
查看更多
淡お忘
5楼-- · 2019-02-05 20:04

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.

查看更多
登录 后发表回答