0

The line containing NSEntityDescription.entity is crashing and giving me the error

Thread 1: SIGABRT

I have seen other people ask this question, the recommended answer is to simply delete and remake the entity from scratch. I have done this many times, I have also "cleaned" the code thoroughly, and imported CoreData in both my AppDelegate.swift files and this ViewController file. Does anyone have any advice?

override func viewDidLoad() {
    super.viewDidLoad()
    addGesture()
    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    let managedContext = appDelegate.persistentContainer.viewContext
    let stringModelEntity = NSEntityDescription.entity(forEntityName: "StringModels", in: managedContext)!
    let stringBundle = NSManagedObject(entity: stringModelEntity, insertInto: managedContext)
    self.getJSON(stringBundle)
    do {
        try managedContext.save()
    } catch let error as NSError {
        print("Could not save. \(error), \(error.userInfo)")
    }
}

Stack Overflow will not let me embed images yet, but here is the Models.xcdatamodeld file.

EDIT

I've found a solution thanks to finally opening the debugger, the following link's 'best answer' describes and solves this issue: Core data: Failed to load model

Angel F Syrus
  • 1,984
  • 8
  • 23
  • 43
mahib
  • 3
  • 4
  • Please provide the crash log. – matt Jul 17 '18 at 23:45
  • @matt There is no crash log, it simply crashes with no output to the console. – mahib Jul 17 '18 at 23:49
  • Okay, but there's still a backtrace. See https://stackoverflow.com/questions/7790993/sigabrt-with-no-error-message. Xcode is giving you lots of info about _why_ we are crashing. You need to look at that info to find out what's going on. – matt Jul 18 '18 at 00:05
  • Are you able to create this or any other entity elsewhere in the app? One thing that I often forget in Swift Core Data code is setting the class module for the entity (in the data model editor) to Current Product Module. – Steve Madsen Jul 18 '18 at 01:58
  • It looks like persistentContainer.storage is nil in your debug session. Is this relevant here or is it for some other code? Perhaps you send a nil managed object context to `NSEntityDescription.entity`? If so the bug seems to be in your AppDelegate class. – Joakim Danielson Jul 18 '18 at 07:35
  • @matt You're totally right (I'm very new to XCode), my debugger was just hiding for a while.. Not sure why I didn't think anything of it. Anyways, I retrieved from the debugger that the error at hand was that in my AppDelegate.swift class, I was not naming the Model properly (see https://stackoverflow.com/q/42553749/10096358). It should be named after the filename, in my case, "Model" instead of StringModels. Now I am not getting this error, so we shall see where it goes from here. – mahib Jul 18 '18 at 16:03
  • @JoakimDanielson you are totally right, the bug is in the AppDelegate class because in there the CoreDataModel is trying to find "StringModels" which is the Entity name, but it really needs the name of the xcdatamodeld file (in my case, "Model"). – mahib Jul 18 '18 at 16:04

1 Answers1

1

This will mean that it cannot find the entity with the name "StringModels". In my experience, the error SIGABRT is caused when something that the program thinks should exist does not.

I would check capitalization and spelling.

  • Thank you for your answer. The Entity in Model.xcdatamodeld is listed as "StringModels" -- exactly as specified above. – mahib Jul 17 '18 at 23:45
  • @mahib When you crash, can you check that your 'managedContext' constant isn't nil inside the debug area? – Gabriel Wong Jul 18 '18 at 00:14
  • the managedContext is nil as shown above in the screenshot, I fixed the error by correcting the name "StringModels" (which is the Entity name) to "Model" which is the name of the file Model.xcdatamodeld and contains the Entity StringModels. – mahib Jul 18 '18 at 16:10