0

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

0

1 Answer 1

1

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")
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ok will this reset all the values for delivery
I think the right question to ask is is there a way to overwrite data in an attribute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.