fetchedResultsController 返回 nil
Posted
技术标签:
【中文标题】fetchedResultsController 返回 nil【英文标题】:fetchedResultsController returns nil 【发布时间】:2017-07-05 16:01:02 【问题描述】:尝试创建简单的应用程序以将数据从核心数据显示到表视图。代码构建成功 - 每次加载时都会添加新条目,目前超过 20 个 - 但是在尝试构建表时它会崩溃。
这里有 ViewController:
import UIKit
import CoreData
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
@IBOutlet weak var habitTable: UITableView!
override func viewDidLoad()
super.viewDidLoad()
let habitClassName:String = String(describing: Habits.self)
let habit:Habits = NSEntityDescription.insertNewObject(forEntityName: habitClassName, into: DatabaseController.getContext()) as! Habits
habit.habitsPic = ":("
habit.positive = false
habit.theHabit = "do bad"
DatabaseController.saveContext()
let fetchRequest:NSFetchRequest<Habits> = Habits.fetchRequest()
do
let searchResults = try DatabaseController.getContext().fetch(fetchRequest)
print("number of results: \(searchResults.count)")
catch
print("Error \(error)")
//CONSTRUCT TABLE VIEW
func numberOfSectionsInTableView(tableView: UITableView)-> Int
return fetchedResultsController.sections!.count
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
guard let sections = fetchedResultsController.sections else
fatalError("No sections in fetchedResultsController")
let sectionInfo = sections[section]
return sectionInfo.numberOfObjects
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)-> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "positiveCell", for: indexPath as IndexPath)
let habit = fetchedResultsController.object(at: indexPath as IndexPath) as! Habits
cell.textLabel?.text = habit.theHabit
cell.detailTextLabel?.text = habit.habitsPic
return cell
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
//FOR DISPLAYING DATA IN TABLE
var fetchedResultsController: NSFetchedResultsController<NSFetchRequestResult>!
func initializeFetchedResultsController()
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Habits")
let departmentSort = NSSortDescriptor(key: "theHabit", ascending: true)
let lastNameSort = NSSortDescriptor(key: "positive", ascending: true)
request.sortDescriptors = [departmentSort, lastNameSort]
let moc = DatabaseController.getContext()
fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = (self as! NSFetchedResultsControllerDelegate)
do
try fetchedResultsController.performFetch()
catch
fatalError("Failed to initialize FetchedResultsController: \(error)")
和数据库控制器:
import Foundation
import CoreData
class DatabaseController
private init()
class func getContext() -> NSManagedObjectContext
return DatabaseController.persistentContainer.viewContext
let container = NSPersistentContainer(name: "ada")
container.loadPersistentStores(completionHandler: (storeDescription, error) in
if let error = error as NSError?
fatalError("Unresolved error \(error), \(error.userInfo)")
)
return container
()
// MARK: - Core Data Saving support
class func saveContext ()
let context = persistentContainer.viewContext
if context.hasChanges
do
try context.save()
catch
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
它不会构建表格视图,因为 。我认为这是“moc”变量的问题,但是什么?
编辑
错误: 致命错误:在展开可选值时意外发现 nil
行: 守卫让sections = fetchedResultsController.sections else
【问题讨论】:
"但是在尝试构建表时它会崩溃。"错误信息是什么? 什么时候调用initializeFetchedResultsController
方法?
@Larme 错误消息:致命错误:在展开可选值时意外发现 nil
@TomHarrington 我叫它守卫让sections = fetchedResultsController.sections else
@MartinsB 然后更新您的示例代码以使其准确,因为您的问题没有显示出来。
【参考方案1】:
您将获取的结果控制器声明为隐式展开的可选:
var fetchedResultsController: NSFetchedResultsController<NSFetchRequestResult>!
它的初始值为nil。
您还有一个名为initializeFetchedResultsController
的方法,它为该属性分配一个值,但您永远不会调用该方法。在评论中你说你在这段代码中调用它:
guard let sections = fetchedResultsController.sections else
fatalError("No sections in fetchedResultsController")
但该代码从未提及initializeFetchedResultsController
方法,因此它从未调用该方法。
你有一个可选变量,但你从来没有给它赋值。所以它永远不会获得价值。所以它是零。这不是核心数据问题,而是基本的 Swift。除非您给属性赋值,否则属性不会获得值,而您并没有这样做。
【讨论】:
以上是关于fetchedResultsController 返回 nil的主要内容,如果未能解决你的问题,请参考以下文章
FetchedResultsController 中没有部分
如何将对象从 fetchedResultsController 到 Plist?
fetchedResultsController 对象的表视图部分
fetchedResultsController 和 Integer