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