I am doing core data in Swift 3. I am able to store data and able to fetch, working fine. But, while trying to delete particular data suppose string data, its not able to do.
Following is my code
//Delete if existing data there
let context = DatabaseController.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "UserData")
fetchRequest.predicate = NSPredicate.init(format: "userFocusData==\(node.label.text!)")
let result = try? context.fetch(fetchRequest)
let resultData = result as! [UserData]
for object in resultData {
context.delete(object)
}
do {
try context.save()
print("saved!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
self.showAlert(message: "Data not able to save, please try again later.", title: kText_AppName)
} catch {
}
And its getting crash at following line
let result = try? context.fetch(fetchRequest)
And crash report is
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath Sharepoint not found in entity <NSSQLEntity UserData id=6>'
But, I am passing some string, that string I want to delete from database. Can anyone suggest me, how to fix this, I have not used core data well.
try context.save()code inside the for loop, or find what object you want to delete and take for loop out.