Delete/Reset all entries in Core Data?

后端 未结 30 3165
天命终不由人
天命终不由人 2020-11-22 16:00

Do you know of any way to delete all of the entries stored in Core Data? My schema should stay the same; I just want to reset it to blank.


Edit

30条回答
  •  余生分开走
    2020-11-22 16:13

    here my swift3 version for delete all records. 'Users' is entity name

    @IBAction func btnDelAll_touchupinside(_ sender: Any) {
    
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let managedObjectContext = appDelegate.persistentContainer.viewContext
    
        let fetchReq = NSFetchRequest(entityName: "Users")
        let req = NSBatchDeleteRequest(fetchRequest: fetchReq)
    
        do {
            try managedObjectContext.execute(req)
    
        } catch {
            // Error Handling
        }   
    }
    

提交回复
热议问题