Reputation: 25
How can i delete all the data in a particular attribute in an entity i don't want to empty all the attributes in the entity here is what i have now which deletes all the attributes in an entity
func deletObject(){
let fetchRequest = NSFetchRequest()
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
fetchRequest.includesPropertyValues = false
let context:NSManagedObjectContext = appDel.managedObjectContext
let moc = context
fetchRequest.entity = NSEntityDescription.entityForName("Cart", inManagedObjectContext: moc)
do {
if let results = try moc.executeFetchRequest(fetchRequest) as? [NSManagedObject] {
for result in results {
moc.deleteObject(result)
}
try moc.save()
self.cart = [Cart]()
self.tableView.reloadData()
totalAmouLa.text = "₦\(Int(totalHoursWorkedSum))"
}
} catch {
print("FAILED")
}
}
EDIT
I want to delete delivery
in the Cart
entity
Image
Upvotes: 0
Views: 42
Reputation: 285270
You cannot delete an attribute from the Core Data model, you can only reset the value.
executeFetchRequest
returns guaranteed [Cart]
on success, the optional binding will never fail
func deletObject(){
let fetchRequest = NSFetchRequest()
let appDel = UIApplication.sharedApplication().delegate as! AppDelegate
let context = appDel.managedObjectContext
fetchRequest.entity = NSEntityDescription.entityForName("Cart", inManagedObjectContext: context)
do {
let results = try context.executeFetchRequest(fetchRequest) as! [Cart]
results.forEach { $0.delivery = 0.0 }
try context.save()
self.tableView.reloadData()
totalAmouLa.text = "₦\(Int(totalHoursWorkedSum))"
} catch {
print("FAILED")
}
}
Upvotes: 1