0

My app is crashing and giving me a fatal error that doesn't really tell me anything so I don't know what could be happening.

Here's the section of my code that's giving me trouble.

var controller: NSFetchedResultsController<Item>!


func attemptFetch() {

    let fetchRequest:  NSFetchRequest<Item> = Item.fetchRequest()
    let dateSort = NSSortDescriptor(key: "created", ascending: false)
    fetchRequest.sortDescriptors = [dateSort]

    let mainController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)

    self.controller = mainController

    do {
        try mainController.performFetch()
    } catch {

        let error = error as NSError
        print("\(error)")

    }

}

It crashes at

let mainController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)

And "context" is referencing the app delegate

let ad = UIApplication.shared.delegate as! AppDelegate
let context = ad.persistentContainer.viewContext

The error is :

fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=134140 "(null)" UserInfo={sourceModel=() isEditable 1, entities { "Crash on line 70 of the app delegate"

Thanks for your help

Punith R Kashi
  • 139
  • 1
  • 10
  • `controller` (`NSFetchedResultsController`) is supposed to be a lazy instantiated property in the class as suggested by Apple in the Core Data template. – vadian Feb 15 '17 at 21:01
  • @vadian Could you provide the link for the Apple documentation where it says that it's supposed to be lazy instantiated? –  Feb 15 '17 at 21:02
  • Just create a new (Master-Detail) project with Core Data enabled and study the provided code. – vadian Feb 15 '17 at 21:03
  • @vadian I know how the demo products look, but where is the documentation for how it is supposed to be and that it should make any difference not doing a lazy instantiating? I am asking out of personal interest since I have not read this anywhere and IF this is true what you say ,I might have to redo some work in my own project. But as far as I know, everything works fine for me. –  Feb 15 '17 at 21:07
  • http://stackoverflow.com/questions/18150816/mapping-model-nowhere-to-be-found-cocoa-error-134140 – user3581248 Feb 15 '17 at 21:09
  • @Sneak I assume the suggested code in the template is the code to be supposed. Even in the older Objective-C version the results controller is created lazily. Additionally in Swift the lazy instantiation makes the property non-optional which is another benefit. – vadian Feb 15 '17 at 21:14
  • @vadian Well, I want to know why it isn't documented and what differences it makes. Just because the demo templates or demo code provides with some code doesn't mean it has to be done that way, for some cases it might. I tried to google madly and couldn't find one single thing about it. Also, reading their documentation: https://developer.apple.com/reference/coredata/nsfetchedresultscontroller?language=objc you can see the example code there if you scroll down a bit, not a single word or code mentioned about this, so I guess it does not make any difference. –  Feb 15 '17 at 21:18
  • Try this inside of the do block: try self.controller.performFetch() – Mannopson Feb 16 '17 at 01:18

1 Answers1

0

Since you are running this fetch in your AppDelegate methods, I suspect that you are trying to do your fetch before your CoreData Persistent Store and it's contextes has been initialized resulting in the crash.