Currently, I just started using sectionNameKeyPath from NSFetchedResultsController in Swift's core data /
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)
}
}