删除 CoreData 对象

Posted

技术标签:

【中文标题】删除 CoreData 对象【英文标题】:Delete CoreData object 【发布时间】:2018-03-05 11:59:01 【问题描述】:

我还在为CoreData 努力开始新的一周哈哈。我终于成功保存和获取了我的数组,现在是时候编辑和删除了。

我首先添加了删除功能,但在传递正确的参数时遇到了问题:

核心数据函数:

class CDHandler: NSObject 

private class func getContext() -> NSManagedObjectContext 
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    return appDelegate.persistentContainer.viewContext


class func saveObject(name:String, code:String, symbol:String, placeholder:String, amount:String) -> Bool 
    let context = getContext()
    let entity = NSEntityDescription.entity(forEntityName: "CryptosMO", in: context)
    let managedObject = NSManagedObject(entity: entity!, insertInto: context)

    managedObject.setValue(name, forKey: "name")
    managedObject.setValue(code, forKey: "code")
    managedObject.setValue(symbol, forKey: "symbol")
    managedObject.setValue(placeholder, forKey: "placeholder")
    managedObject.setValue(amount, forKey: "amount")

    do 
        try context.save()
        return true
     catch 
        return false
    


class func fetchObject() -> [CryptosMO]? 
    let context = getContext()
    var cryptos: [CryptosMO]? = nil

    do 
        cryptos = try context.fetch(CryptosMO.fetchRequest()) as? [CryptosMO]
        return cryptos
     catch 
        return cryptos
    


class func deleteObject(crypto: CryptosMO) -> Bool 
    let context = getContext()
    context.delete(crypto)

    do 
        try context.save()
        return true
     catch 
        return false
    

创建和保存数组:

    if addedCrypto != "" 
        if addedCrypto == "Bitcoin BTC" 
            if CDHandler.saveObject(name: "Bitcoin", code: "bitcoin", symbol: "BTC", placeholder: "BTC Amount", amount: "0.0") 
                for crypto in CDHandler.fetchObject()! 
                    print("\(String(describing: crypto.name))'s symbol is \(String(describing: crypto.symbol))")
                
            
        
    

获取 TableView 的核心数据:

override func viewWillAppear(_ animated: Bool) 
    tableView.delegate = self
    tableView.dataSource = self


    if CDHandler.fetchObject() != nil 
        cryptos = CDHandler.fetchObject()!
        tableView.reloadData()
    

TableView 函数:

extension WalletTableViewController: UITableViewDelegate, UITableViewDataSource, CryptoCellDelegate 


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return cryptos.count

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! WalletTableViewCell

    cell.cryptoNameLabel.text = cryptos[indexPath.row].name
    cell.amountTextField.placeholder = cryptos[indexPath.row].placeholder

    cell.delegate = self
    cell.amountTextField.delegate = self

    return cell

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) 
    if editingStyle == .delete 
        cryptos.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
        CDHandler.deleteObject(crypto: cryptos) // <----- Cannot convert value of type '[CryptosMO]' to expected argument type 'CryptosMO'
    

这里有什么问题?我可以将func deleteObject(crypto: CryptosMO) 更改为func deleteObject(crypto: [CryptosMO]),然后我得到Cannot convert value of type '[CryptosMO]' to expected argument type 'NSManagedObject'

我读到delete() 仅将NSManagedObject 作为其唯一参数,所以我相信我首先创建了一个不正确的对象才能删除它??

【问题讨论】:

【参考方案1】:

只需调用此方法并传递要删除的managedObject实体即可:

func deleteData(entity:String,deleteObject:NSManagedObject)
    //for ios 10+
           // let delegate = UIApplication.shared.delegate as? AppDelegate
           // let context = delegate!.persistentContainer.viewContext
            let context = getContext()
            context.delete(deleteObject)
            do 
                try context.save()
             catch let error as NSError 
                print("Could not save. \(error), \(error.userInfo)")
            

        


 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) 
        if editingStyle == .delete 
        let selectedManagedObject = cryptos[indexPath.row]
        deleteData(entity:"yourEntityName",deleteObject: selectedManagedObject)
            cryptos.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        
    

与保存方法相同,您可以编辑,只需传递您要编辑的managedObject

class func updateObject(name:String, code:String, symbol:String, placeholder:String, amount:String,selectedManagedObject:NSManagedObject) 
    let context = getContext()

    selectedManagedObject.setValue(name, forKey: "name")
    selectedManagedObject.setValue(code, forKey: "code")
    selectedManagedObject.setValue(symbol, forKey: "symbol")
    selectedManagedObject.setValue(placeholder, forKey: "placeholder")
    selectedManagedObject.setValue(amount, forKey: "amount")

    do 
        try context.save()
     catch let error as NSError 
        print("Could not save. \(error), \(error.userInfo)")
    


【讨论】:

非常感谢 Shabbir,这很好用!我可以请您提供服务吗?如果您可以添加 edit 的方法,那就太棒了!【参考方案2】:

当您调用CDHandler.deleteObject(crypto: ...) 时,只需传递(crypto: cryptos[indexPath.row]) 而不是(crypto: cryptos)

    ...
    CDHandler.deleteObject(crypto: cryptos[indexPath.row])
    cryptos.remove(at: indexPath.row)
    tableView.deleteRows(at: [indexPath], with: .fade)

【讨论】:

非常感谢,但我收到了Cannot subscript a value of type '[CryptosMO]' with an index of type 'IndexPath' 对不起,indexPath.row @martin 应该是cryptos[indexPath.row]

以上是关于删除 CoreData 对象的主要内容,如果未能解决你的问题,请参考以下文章

CoreData不删除对象一对多实体

从 Coredata 中删除一个对象(出现错误)

删除CoreData对象

CoreData - 如何使用 validateForDelete:确定是不是应删除托管对象

从 CoreData 中删除所有对象后 NSArrayController 一直为空

CoreData 删除单个对象