Hello.
First of all excuse my english, I am a frensh educated.
It has been one month now since i try to figure out a solution to my pb.
I am also new to Ios developpement: this is my first App.
what I am trying to do is a simple application with core Data.
My application has two entity : TblCategorie and TblProduits ( one to many relation).
In my table view I wanted a sections by categorie, so i found that i can use NSFetchedResultsController to make things easy.
my application work fine, the tableview is correctly populated.
the pb that I have is when i try to reorder rows using drag drop ( not in editing mode ) the application crash: I am using UITableViewDropDelegate,UITableViewDragDelegate.
@IBOutlet var TblView_Produit: UITableView!
let Mycontext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let Myrequest : NSFetchRequest<TblProduits> = TblProduits.fetchRequest()
var fetchedResultsController : NSFetchedResultsController<TblProduits>!
var managedObjectContext: NSManagedObjectContext? = nil// loading Data
func Fct_loadListDesProduits () {
let MySortDescriptor = NSSortDescriptor(key: #keyPath(TblProduits.categorie.categorie_Name), ascending: true)
Myrequest.sortDescriptors = [MySortDescriptor]
fetchedResultsController = NSFetchedResultsController(fetchRequest: Myrequest, managedObjectContext: Mycontext, sectionNameKeyPath: #keyPath(TblProduits.categorie.categorie_Name), cacheName: nil)
do {
try fetchedResultsController.performFetch()
} catch {
debugPrint ("there is an error \(error.localizedDescription)")
}
}// TableView implementation
func numberOfSections(in tableView: UITableView) -> Int {
guard let sections = self.fetchedResultsController.sections else {
return 0
}
print (sections.description)
return sections.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ProduitTVC
if let ProduitName = self.fetchedResultsController.object(at: indexPath).produit_name { /
cell.SetListeDesProduits(ProduitName: ProduitName)
}
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let sections = self.fetchedResultsController.sections else {
return 0
}
return sections[section].numberOfObjects
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
guard let sections = self.fetchedResultsController.sections else {
return ""
}
return sections[section].name
}// implementing NSFetchedResultsController
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch(type) {
case .insert:
if let Myindexpath = newIndexPath {
self.TblView_Produit.insertRows(at: [Myindexpath], with: .left)
}
break;
case .delete:
if let MyindexPath = indexPath {
TblView_Produit.deleteRows(at: [MyindexPath], with: .fade)
}
break;
case .move:
if let MyindexPath = indexPath {
TblView_Produit.deleteRows(at: [MyindexPath], with: .fade)
}
if let MynewIndexPath = newIndexPath {
TblView_Produit.insertRows(at: [MynewIndexPath], with: .fade)
}
break;
case .update:
if let MyindexPath = indexPath {
/
/
}
break;
}
}// the crashing part
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
self.fetchedResultsController.delegate = nil
var objects = self.fetchedResultsController.fetchedObjects
let object = objects![sourceIndexPath.row]
objects?.remove(at: sourceIndexPath.row)
objects?.insert(object, at: destinationIndexPath.row)
Fct_SaveListDesProduits()
self.fetchedResultsController.delegate = self
}what i am doing wrong :
in fact after the dropping my core date isn’t update and i have the error (below) at numberOfRowsInSection
here is the error:
* Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.33.6/UITableView.m:2011 2018-01-26 19:56:05.513608+0100 ShopTogether[8815:1168659] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (1 moved in, 0 moved out).'