0

Currently, I just started using sectionNameKeyPath from NSFetchedResultsController in Swift's core data /

image of my app

Initially, I didn't know how to separate the different cells into their respective dates. But now that I've separated the cells into the respective dates, their indexPath is all screwed up as when I perform a segue to lead to a second viewController, the values are messed up.

For example

  • the second $42.5 -> $9.0,
  • the $10 -> $9.0,
  • $72.0 -> $42.5,
  • $0.0 -> $99.0 and $9.0 -> $10.0

It would really help if anyone can teach me how to properly adjust the indexPath to fit each section after fetching from coreData.

    func initialFetch() {
    let fetchRequest: NSFetchRequest<Item> = Item.fetchRequest()
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "dates", ascending: false)] //sorting according to date


    let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: "dates", cacheName: nil)

    controller.delegate = self

    self.controller = controller
    do {
        try controller.performFetch()
    } catch {
        fatalError("Failed to fetch entities: \(error)")
    }
}

Here's my code to fetch the data

P.S. I think this may have something to do with my 'didSelectRowAt'

    func tableView(_ tableView: UITableView, didSelectRowAt      indexPath: IndexPath) {
    if let objs = controller.fetchedObjects, objs.count > 0 {
        let item = objs[indexPath.row]
        performSegue(withIdentifier: "TrackerVC", sender: item)
    }
}

2 Answers2

0

Your sort descriptor need to include both the field that is the section and for the rows within the section so add a sort descriptor for the price property to your existing one.

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • I tried adding this but it still doesn't work '' fetchRequest.sortDescriptors = [NSSortDescriptor(key: "dailySpending", ascending: false)] '' –  May 07 '18 at 11:25
  • @Kendrew Add, not replace. `fetchRequest.sortDescriptors = [NSSortDescriptor(key: "dates", ascending: false), NSSortDescriptor(key: "dailySpending", ascending: false)]` – Joakim Danielson May 07 '18 at 12:06
  • hi, thanks for the reply. I've tried that before too, it helps to sort my rows by their spending but it doesn't solve the problem stated above –  May 12 '18 at 06:57
0

Ok after many weeks of trial and error as well as research, I've finally found the simplest solution to this.

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let item = controller.object(at: indexPath)
    performSegue(withIdentifier: "TrackerVC", sender: item)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "TrackerVC" {
        if let destination = segue.destination as? TrackerVC {
            if let item = sender as? Item {
                destination.itemToEdit = item
            }
        }
    }
}

so this is the simplest solution I found to perform the segue according to the rows in each section