如何在使用持久容器的表视图中删除CoreData条目?

时间:2019-06-18 06:12:52

标签: ios arrays swift uitableview core-data

我正在设计一个应用程序,用户将在具有多个文本字段的警报视图中键入信息。然后将这些字符串组合并保存在实体(作为一个实体)的属性中。然后将其显示在表格视图中。

现在,如果用户输入错误,我将实现一种删除条目的方法。

我已经看到很多地方,他们说使用以下代码删除CoreData中的条目:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
    let managedObjectContext = coreData.persistentContainer.viewContext

    if editingStyle == .delete
    {
        movieToDelete = fetchedResultController.object(at: indexPath)

        let confirmDeleteAlertController = UIAlertController(title: "Remove Movie", message: "Are you sure you would like to delete \"\(movieToDelete!.title!)\" from your movie library?", preferredStyle: UIAlertControllerStyle.actionSheet)

        let deleteAction = UIAlertAction(title: "Delete", style: UIAlertActionStyle.default, handler: { [weak self] (action: UIAlertAction) -> Void in
            managedObjectContext.delete((self?.movieToDelete!)!)
            self?.coreData.saveContext()
            self?.movieToDelete = nil
        })

        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: { [weak self] (action: UIAlertAction) -> Void in
            self?.movieToDelete = nil
        })

        confirmDeleteAlertController.addAction(deleteAction)
        confirmDeleteAlertController.addAction(cancelAction)

        present(confirmDeleteAlertController, animated: true, completion: nil)

    }
}

但是,我正在关注youtube上的教程,因此我的代码中没有ManagedObjectContext来使用editingStyle.delete。现在我被困在如何删除条目上。我是否可以更改先前的代码以支持ManagedObjectContext,还是可以通过持久性容器删除条目?

这是我的viewController中的代码,我认为这对当前的问题很重要:

class ViewController: UITableViewController {

    var alarmItems: [NSManagedObject] = []
    let cellId = "cellId"


    override func viewDidLoad() {
        super.viewDidLoad()



    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
        let managedContext = appDelegate.persistentContainer.viewContext
        let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "AlarmItems")
        do {
            alarmItems = try managedContext.fetch(fetchRequest)
        } catch let err as NSError {
            print("Failed to fetch items", err)
        }
    }

    @objc func addAlarmItem(_ sender: AnyObject) {
        print("this works")
        let alertController = UIAlertController(title: "Add New Item", message: "Please fill in the blanks", preferredStyle: .alert)
        let saveAction = UIAlertAction(title: "Save", style: .default) { [unowned self] action in

            //combined string of attributes
            let myStrings: [String] = alertController.textFields!.compactMap { $0.text }
            let myText = myStrings.joined(separator: ", ")

            self.save(myText)
            self.tableView.reloadData()
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)

        alertController.addTextField { (textField) in
            textField.placeholder = "Enter Name of Engineer"
        }
        alertController.addTextField { (textField) in
            textField.placeholder = "Enter Date of Alarm in DD/MM/YYYY"
        }
        alertController.addTextField { (textField) in
            textField.placeholder = "Enter Time of Alarm in 24h (eg: 2300)"
        }
        alertController.addTextField { (textField) in
            textField.placeholder = "Please indicate True/False (type True or False)"
        }
        alertController.addTextField { (textField) in
            textField.placeholder = "Insert comments (if any), or NIL"
        }


        alertController.addAction(saveAction)
        alertController.addAction(cancelAction)
        present(alertController, animated: true, completion: nil)
    }

    func save(_ itemName: String) {
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
        let managedContext = appDelegate.persistentContainer.viewContext
        let entity = NSEntityDescription.entity(forEntityName: "AlarmItems", in: managedContext)!
        let item = NSManagedObject(entity: entity, insertInto: managedContext)
        item.setValue(itemName, forKey: "alarmAttributes")

        do {
            try managedContext.save()
            alarmItems.append(item)

        } catch let err as NSError {
            print("Failed to save an item", err)
        }
    }

    @objc func exportCSV(_ sender: AnyObject) {
        //will work on exporting csv in the future
        return
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return alarmItems.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        let alarmItem = alarmItems[indexPath.row]
        cell.textLabel?.text = alarmItem.value(forKeyPath: "alarmAttributes") as? String
        return cell
    }

    /*
    //create delete feature

   override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath ) {
        if editingStyle == UITableViewCell.EditingStyle.delete {


        }
    }
 */

}

1 个答案:

答案 0 :(得分:0)

在使用NSFetchedResultsController时,删除数据源数组

var alarmItems: [NSManagedObject] = []  

alarmItems的所有其他出现。 NSFetchedResultsController实例成为数据源数组

viewWillAppear中只是重新获取数据

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    do {
        try fetchedResultsController.performFetch()
        tableView.reloadData()
    } catch {
        print(error)
    }
}

替换数据源和委托方法
override func numberOfSections(in tableView: UITableView) -> Int {
    return fetchedResultsController.sections?.count ?? 0
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
    let alarmItem = fetchedResultsController.object(at: indexPath) as! NSManagedObject
    cell.textLabel?.text = alarmItem.value(forKeyPath: "alarmAttributes") as? String
    return cell
}

现在您的删除方法应该可以使用了。

注意:

我们鼓励您始终使用NSManagedObject子类AlarmItems(例如,每个Core Data记录在语义上表示一个AlarmItem)和点表示法。

let alarmItem = fetchedResultsController.object(at: indexPath) as! AlarmItem
cell.textLabel?.text = alarmItem.alarmAttributes