I have a problem, my objects every time I save them are reflected in uitableview in a duplicate way, does anyone know how to solve it or if I have a problem in my code?
I have tried to solve the error and I have investigated what it is for my array but the truth is that I have not had the solution no matter how much I look for it, if someone could help me it would be of great help.
Code Block import UIKit import CoreData import Foundation class ViewController: UIViewController { //MARK:= Outles @IBOutlet var tableView: UITableView! //MARK:= variables var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext var items: [Entity]? var duplicateName:String = "" //MARK:= Overrides override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self fetchPeople() addPeople(context) print(" nombres: \(items?.count)") } override func viewWillAppear(_ animated: Bool) { } //MARK:= Core Data funcs func fetchPeople(){ do{ self.items = try! context.fetch(Entity.fetchRequest()) DispatchQueue.main.async { self.tableView.reloadData() } }catch let error as NSError{ print("Tenemos este error \(error.debugDescription)") } } func addPeople(_ contexto: NSManagedObjectContext) { let usuario = Entity(context: contexto); usuario.nombre = "Valeria"; usuario.edad = 25; usuario.eresHombre = false; usuario.origen = "Ensenada,B.C" usuario.dia = Date(); do{ try! contexto.save(); }catch let error as NSError{ print("tenemos este error en el guardado \(error.debugDescription)"); } fetchPeople() } func deletDuplicates(_ contexto: NSManagedObjectContext){ let fetchDuplicates = NSFetchRequest<NSFetchRequestResult>(entityName: "Persona") // // do { // items = try! (contexto.fetch(fetchDuplicates) as! [Entity]) // } catch let error as NSError { // print("Tenemos este error en los duplicados\(error.code)") // } let rediciendArray = items!.reduce(into: [:], { $0[$1,default:0] += 1}) print("reduce \(rediciendArray)") let sorteandolos = rediciendArray.sorted(by: {$0.value > $1.value }) print("sorted \(sorteandolos)") let map = sorteandolos.map({$0.key}) print(" map : \(map)") } } // End of class
I have tried to solve the error and I have investigated what it is for my array but the truth is that I have not had the solution no matter how much I look for it, if someone could help me it would be of great help.