2015-05-05 60 views
0

我在創建我的NSFetchedResultsController時收到objc_exception_throw。我對iOS編程相對比較陌生,所以我仍然在調試iOS崩潰。NSFetchedResultsController在創建時崩潰,objc_exception_throw

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

堆棧跟蹤和錯誤,我們不是魔術師:) – Schemetrical

+0

@Schemetrical什麼?!你不是魔術師?!爲什麼我甚至在這裏呢!哈哈我真的明白了,我沒有在那裏放置一個排序描述符。我會盡我所能做出自己的答案。 –

回答

1

我在我的提取請求中丟失了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 
}()