Ios Swift,使用核心数据清除主详细信息中的tableview时出错,tableview加载不存在实体的单元格?

Ios Swift,使用核心数据清除主详细信息中的tableview时出错,tableview加载不存在实体的单元格?,ios,uitableview,core-data,swift3,uisplitviewcontroller,Ios,Uitableview,Core Data,Swift3,Uisplitviewcontroller,我目前正在学习Swift 3以完成一项作业 我已经在Xcode中创建了一个主视图原型。它使用自定义单元格(名称和时间戳只有两个标签)从核心数据(自定义事件)加载一个实体 当前,它删除上下文中的所有现有实体,然后生成5个新实体: override func viewDidLoad() { super.viewDidLoad() // ... // removes all items from the database core let context = self

我目前正在学习Swift 3以完成一项作业

我已经在Xcode中创建了一个主视图原型。它使用自定义单元格(名称和时间戳只有两个标签)从核心数据(自定义事件)加载一个实体

当前,它删除上下文中的所有现有实体,然后生成5个新实体:

override func viewDidLoad() {
    super.viewDidLoad()
    // ...

    // removes all items from the database core
    let context = self.fetchedResultsController.managedObjectContext
    let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Event")
    let request = NSBatchDeleteRequest(fetchRequest: fetch)
    do
    {
        // not sure what to do with result
        let _ = try context.execute(request)
        try context.save()

        // get count of event entities from core context...
        print("entities in context: \(try context.count(for: NSFetchRequest<NSFetchRequestResult>(entityName: "Event")))")
    }
    catch
    {
        let nserror = error as NSError
        print("encountered error clearing all existing events from app Core Data.")
    }

    // load a fresh batch of items
    insertFiveNewEvents()
}

func insertFiveNewEvents() {
        let context = self.fetchedResultsController.managedObjectContext
        for c in 1...5
        {
            let newEvent = Event(context: context)
            // If appropriate, configure the new managed object.
            newEvent.name = "Item" + String(c)
            newEvent.timestamp = NSDate()

            // Save the context.
            do {
                try context.save()
                print("entities in context: \(try context.count(for: NSFetchRequest<NSFetchRequestResult>(entityName: "Event")))")
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }
    }
表格加载单元格时出错

fatal error: unexpectedly found nil while unwrapping an Optional value
控制台和碰撞点的屏幕剪切:

因此,它正在加载和配置单元,它配置了前5个fine(核心中的5个新事件),但随后它尝试加载另一个

调试器监视SOW该event.name!无效,表明事件未引用现有实体

我不确定这些实体是从哪里来的

可能性:

  • 我没有正确处理核心上下文

  • 在删除和添加事件后,我必须对UITableViewController执行一些操作。我以前确实尝试过tableView.reload(),但没有效果

  • tableview认为单元格多于事件

调试似乎还显示,当fetchedResultsController被要求提供一个节、每个节的计数和行计数时,它将返回多个节,每个节有8行。我不确定这是否是一个问题(因为我不完全理解tableview的节和行)


建议?

提供一个错误。您能显示插入5个新元素的函数吗?听起来您在使用2个不同的上下文,可能尝试将fetch results上下文传递到func.Edited、added错误和insertNewEntities函数中。
fatal error: unexpectedly found nil while unwrapping an Optional value