Ans:
Deleting a record from a persistent store involves three steps:
Deleting a record from a persistent store involves three steps:
- Fetch the record that needs to be deleted
- Mark the record for deletion
- Save the changes
Code :
let fetchRequest: NSFetchRequest<Profile> = Profile.fetchRequest()
fetchRequest.predicate = Predicate.init(format: "profileID==\(withID)")
let objects = try! context.fetch(fetchRequest) // Step 1
for obj in objects {
context.delete(obj) // Step 2
}
do {
try context.save() // <- remember to save data // Step 3
} catch {
// Do something...
}