无法从核心数据中删除swift4

时间:2018-05-29 08:57:14

标签: core-data swift4

我无法从核心数据中删除数据。以下是我的代码。当我删除时,数据会从表中删除,但是一旦我向后导航,它就会再次显示,因为它不会从核心数据中删除。感谢

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
            let data = (fav[indexPath.row])
            if editingStyle == .delete {
                  DataSave.deleteData(dataToDelete: data)
                fav.remove(at: indexPath.row)
                tableView.deleteRows(at: [indexPath], with: .automatic)


            }
            self.tableView.reloadData()
        }

**My deleteData function** - 


    class func deleteData(dataToDelete:String) {
            let context = getContext()

            let fetchRequest = NSFetchRequest<City>(entityName: "City")
            if let fetchResults = try!context.fetch(fetchRequest as! NSFetchRequest<NSFetchRequestResult>) as? [City] {
                for result in fetchResults{
                    if result.city_name == [dataToDelete] {
                        context.delete(result)
                        print("deleted from cored data")
                        do {
                            try context.save() 
                        } catch {
                            print("could not delete from core data")
                        }
                    }
                }
            }
        }

0 个答案:

没有答案