UITableView con Xcode, Problema de obtención de CoreData
He estado trabajando en un proyecto pero tengo problemas para recuperar CoreData en mi TableView. Este es mi código:
“`swift
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
func loadData() {
let requestData: NSFetchRequest
do {
personArr = try context.fetch(requestData)
try context.save()
} catch {
print(“Error al recuperar la solicitud de datos: (error)”)
self.tableView.reloadData()
}
}
// let DVC = segue.destination as! addPersonController
// numberOfRowsInSection … cellForRowAt
// let cell = tableView.dequeueReusableCell(withIdentifier: “personCell”, for: indexPath)
// cell.textLabel?.text = itemArray[indexPath.row].title
// let person = personArr[indexPath.row]
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRow(at: indexPath)?.accessoryType == .checkmark {
tableView.cellForRow(at: indexPath)?.accessoryType = .none
personArr[indexPath.row].finish = false
//cell.accessoryType = .checkmark //forRaw
}
tableView.deselectRow(at: indexPath, animated: true) //not selected
savePerson()
// let myPerson = PersonArr[indexPath.row].name
// performSegue(withIdentifier: “personSegue”, sender: myPerson)
}
Aquí hay un ejemplo de una función que apliqué. Mi preocupación está en recuperar los datos en personArr, ya que no se está recuperando como se esperaba. ¿Alguna idea?
davy.ai
Parece que el código está intentando obtener datos de CoreData utilizando NSFetchRequest, pero no hay una comprobación de errores durante la obtención. Además, no hay indicación de dónde se declara o cómo se utiliza personArr en el código.
Para recuperar datos de CoreData en una TableView, puedes hacer lo siguiente:
Con esta implementación, la TableView se actualizará automáticamente cuando haya cambios en los objetos de CoreData.