嗨我得到了一个错误(FetchedResultsController.getter)。(闭包#1)
错误点是
let fetchedResultsController =
NSFetchedResultsController(fetchRequest: fetchRequest,
managedObjectContext: self.managedObjectContext,
sectionNameKeyPath: nil, cacheName: nil)
我删除了sql文件,并恢复了模拟器,但没有。 我创建了sql文件但是当行执行时我就有了这个错误
这是我到目前为止的代码
var managedObjectContext : NSManagedObjectContext!
lazy var fetchedResultsController : NSFetchedResultsController<WeatherLocation> = {
let fetchRequest = NSFetchRequest<WeatherLocation>()
let entity = WeatherLocation.entity()
fetchRequest.entity = entity
fetchRequest.fetchBatchSize = 20
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController
}()
override func viewDidLoad() {
super.viewDidLoad()
performFetch()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func performFetch(){
do{
try fetchedResultsController.performFetch()
}catch{
fatalError("FatalError : \(error)")
}
}
deinit {
fetchedResultsController.delegate = nil
}
扩展名----
extension PlacesTableViewController : NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
print("***--- controllerWillChangeContent")
tableView.beginUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert :
print("***--- NSFetchedResultsChangeinsert (object)")
tableView.insertRows(at: [indexPath!], with: .fade)
case.delete:
print("***--- NSFetchedResultsChangeDelete (object)")
tableView.deleteRows(at: [indexPath!], with: .fade)
case .update :
print("***--- NSFetchedResultsChangeUpdate (object)")
if let cell = tableView.cellForRow(at: indexPath!) {
let weatherLocation = controller.object(at: indexPath!) as! WeatherLocation
cell.textLabel?.text = weatherLocation.name
cell.detailTextLabel?.text = "Latitude:\(weatherLocation.latitude) , Longitude : \(weatherLocation.longitude)"
}
case .move:
print("***--- NSFetchedResultsChangeMove (object)")
tableView.deleteRows(at: [indexPath!], with: .fade)
tableView.insertRows(at: [indexPath!], with: .fade)
}
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
switch type {
case .insert :
print("***--- NSFetchedResultsChangeInsert (section)")
tableView.insertSections(IndexSet(integer: sectionIndex), with: .fade)
case .delete :
print("***--- NSFetchedResultsChangeDelete (section)")
tableView.deleteSections(IndexSet(integer:sectionIndex), with: .fade)
case .update :
print("***--- NSFetchedResultsChangeUpdate")
case.move:
print("***--- NSFetchedResultsChangeMove")
}
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
print("***--- controllerDidChangeContent")
tableView.endUpdates()
}
}
和AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let placesController = window?.rootViewController as! PlacesTableViewController
placesController.managedObjectContext = managedObjectContext
let _ = placesController.view
print(applicationDocumentsDirectory)
return true
}
lazy var persistentContainer : NSPersistentContainer = {
let container = NSPersistentContainer(name: "DataModel")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error {
fatalError("Could load data store : \(error)")
}
})
return container
}()
lazy var managedObjectContext : NSManagedObjectContext = self.persistentContainer.viewContext
我知道为什么会收到此错误?
由于