NSFetchedResultsController在创建它时崩溃,objc_exception_throw

时间:2015-05-05 00:10:56

标签: ios objective-c core-data nsfetchedresultscontroller

我在创建NSFetchedResultsController时收到 objc_exception_throw 。我对iOS编程比较陌生,所以我仍然在调试iOS崩溃问题。

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
lazy var parentContext: NSManagedObjectContext? = {
    if let managedObjectContext = self.appDelegate.managedObjectContext {
        return managedObjectContext
    }
    else {
        return nil
    }
    }()

lazy var fetchedResultsController: NSFetchedResultsController = {
    let request = NSFetchRequest()
    let entity = NSEntityDescription.entityForName("GameDate", inManagedObjectContext: self.parentContext!)
    request.entity = entity
    let frc = NSFetchedResultsController(fetchRequest: request, managedObjectContext: self.parentContext!, sectionNameKeyPath: nil, cacheName: nil)
    frc.delegate = self
    return frc
}()

override func viewDidLoad() {
    super.viewDidLoad()
    performFetchFromDb()
    setupTableView()
}

我在这条线上遇到了崩溃:

let frc = NSFetchedResultsController(fetchRequest: request, managedObjectContext: self.parentContext!, sectionNameKeyPath: nil, cacheName: nil)

此崩溃是否与我的上下文有关?

1 个答案:

答案 0 :(得分:2)

我的抓取请求中缺少NSSortDescriptor。这修好了它:

lazy var fetchedResultsController: NSFetchedResultsController = {
    let request = NSFetchRequest()

    let entity = NSEntityDescription.entityForName("GameDate", inManagedObjectContext: self.parentContext!)
    request.entity = entity

    let gameTimeSort = NSSortDescriptor(key: "gameTime", ascending: false)
    request.sortDescriptors = [gameTimeSort]

    let frc = NSFetchedResultsController(fetchRequest: request, managedObjectContext: self.parentContext!, sectionNameKeyPath: nil, cacheName: nil)
    frc.delegate = self
    return frc
}()