1

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.

4
  • 2
    try this fetchRequest.predicate = NSPredicate(format: "UserData.userFocusData = %@",node.label.text!) Commented Dec 5, 2017 at 11:08
  • In your code, you are trying to delete array of objects from resultData but only saving the context after all deletions happened. You can try: Add try context.save() code inside the for loop, or find what object you want to delete and take for loop out. Commented Dec 5, 2017 at 11:30
  • @Sahil, thanks, its helped me. Commented Dec 5, 2017 at 11:42
  • You're welcome :) Commented Dec 5, 2017 at 11:57

2 Answers 2

1

In Swift 3
In this Code i am delete data based on id

func deleteTrip(Id:String)
    {
        managedContext = appDelegate.persistentContainer.viewContext
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName:"your entity")
        fetchRequest.predicate = NSPredicate(format: "id = %@", "\(Id)")
        do
        {
            let fetchedResults =  try managedContext!.fetch(fetchRequest) as? [NSManagedObject]

            for entity in fetchedResults! {

                managedContext?.delete(entity)
                do
                {
                    try managedContext.save()



                }


                catch let error as Error!
                {
                    print(error.localizedDescription)
                }


            }
        }
        catch _ {
            print("Could not delete")

        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Issue found.

So, the solution answer is

fetchRequest.predicate = NSPredicate(format: "UserData.userFocusData = %@",node.label.text!)

Comments

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.