currently, I'm trying to fetch an entity of type song on one of my view controllers. this is the code that's relevant, that I have:
import CoreData
class TimerScreenVC: UIViewController, NSFetchedResultsControllerDelegate {
var songController: NSFetchedResultsController<Song>!
override function viewDidLoad() {
super.viewdidLoad()
attemptSongFetch()
}
func attemptSongFetch() {
let fetchRequest: NSFetchRequest<Song> = Song.fetchRequest()
let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
let sortByTitle = NSSortDescriptor(key: "title", ascending: true)
fetchRequest.sortDescriptors = [sortByTitle]
songController = controller
do {
try songController.performFetch()
} catch {
let error = error as NSError
print("\(error)")
}
}
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
print("called will change")
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
print("called did change")
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch (type) {
case .insert:
print("has been called")
default:
print("has been called")
}
}
}
however, when I load this view controller, I'm faced with the error "terminating with uncaught exception of type NSException". I can make the error go away and the program works fine if I comment out attemptSongFetch() in viewDidLoad(), but I need to call that function.
Also I have the exact same function, attemptSongFetch(), with the exact same code on another ViewController & that one isn't crashing. Any ideas? Any help would be greatly appreciated.
update so here's the error, it's telling me that I need to set the sort description, which is strange because it's already defined?:
017-02-20 15:48:21.006 Alarm Clock[10433:158613] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An instance of NSFetchedResultsController requires a fetch request with sort descriptors'
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)