0

I am currently learning Swift 3 for an Assignment.

I have created a master-view prototype in Xcode. It loads one entity from Core-Data (a custom Event) using custom cells (just two labels for a name and timestamp).

currently it deletes all existing entities in the context, then generates 5 new ones:

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)")
            }
        }
    }

console output from the above and insertFiveNewEvents() is as follows:

entities in context: 0     -- after clearing core context
entities in context: 1 
entities in context: 2 
entities in context: 3 
entities in context: 4 
entities in context: 5     -- after inserting new entities

An error is occurring when the table is loading cells.

fatal error: unexpectedly found nil while unwrapping an Optional value

Screen snip of console and crash point:

Snapshot, debugging result, failure to load entity from core data, fond nil when forced unwrapping of object

So it's loading and configuring the cells, it configured the first 5 fine (the five new events in core), but then it tries to load another.

A debugger watch sows that event.name! is invalid, suggesting event does not reference an existing entity.

I am unsure of where it is getting these entities from.

Possibilities:

  • I'm not working with the core context properly.

  • I have to do something with UITableViewController after removing and adding events. I did try tableView.reload() before but it had no effect.

  • the tableview thinks there are more cells than events.

Debugging also seems to show then when the fetchedResultsController is asked for a section, count and row count per section, it is returning multiple sections with 8 rows each. I'm not sure if this is an issue (because I don't fully understand the sections and rows for the tableview).

Suggestions?

rask004
  • 552
  • 5
  • 15

0 Answers0