How to correctly pass NSFetchedResultsController's numberOfObjects from one VC to another?

40 Views Asked by At

I have a VC1 with a UITableView which populate it cells with a NSFetchedResultsController. I want VC1 tableView to place all new added cells to the bottom of tableView, therefore I created a CoreData attribute: itemNumberForVC1(type Int32). The cells can be added only from VC2. That's why I need to pass the numberOfObjects from VC1 method numberOfRowsInSection to VC2.

The numberOfObjects is in the following method of VC1:

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   guard let numberOfObjects = fetchedResultsController.sections?[section].numberOfObjects else {return 0}
   return numberOfObjects
}

I need to use the numberOfObjects in my VC2 didSelectRowAt method:

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let currency = fetchedResultsController.object(at: indexPath)
    currency.isForConverter = !currency.isForConverter

    //currency.itemNumberForVC1 = numberOfObjects - 1 <- calculation: there I should receive the actual 
    //number of objects from VC1 and assign the number to picked currency, and it should appear in VC1 
    //at the bottom of its TableView

    coreDataManager.save()
}

I tried to use delegates but inside VC2 didSelectRowAt the result is always 0. The max result I've got is created a method in VC2, which takes numberOfObjects as a parameter, call it in VC1 and then I have that number inside the VC2 but only in borders of the method. I can't move it to VC2 didSelectRowAt (tried through a global variable).

How can I do that efficiently?

0

There are 0 best solutions below