title: Swift 3: How to delete the single record in the Core Data when a UIbutton is Pressed
body: <p>I have a function, where I can add the Profile to the favourite list (which saves through Core Data) and when I want to mark it as Not Favourite, it should delete from the Core data and also from the TableList View (Favourite View Controller). </p>
  
  <p>The code what I have so far, deletes all the records from the list instead deleting the specific record.</p>
  
  <p>Please Find the code Below:-</p>
  
  <pre><code> @IBAction func saveFav(_ sender: UIButton) {
  
     let propertyToCheck = sender.currentTitle!
      var proID = saved_id
      let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
      let task = FavProfile(context: context)
  
      switch propertyToCheck {
      case "Add to Favourite":
       // Link Task &amp; Context
      task.busName = bussinessName
      task.profileID = Int32(id!)!
      print ("saved id is: - \(task.profileID)")
      print ("saved profile name is: - \(task.busName)")
      fav_remove_fav_button_label.setTitle("Remove From Favourite", for: .normal)
  
      // Save the data to coredata
      (UIApplication.shared.delegate as! AppDelegate).saveContext()
  
      let _ = navigationController?.popViewController(animated: true)
  
      let alert = UIAlertController(title: "Alert", message: "Added to your Favourite list", preferredStyle: UIAlertControllerStyle.alert)
      alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
      self.present(alert, animated: true, completion: nil)
  
          case "Remove from Favourite":
  
  
  
              let fetchRequest = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: "FavProfile")
              let moc = getContext()
              let result = try? moc.fetch(fetchRequest)
              let resultData = result as! [FavProfile]
  
              for object in resultData {
                  moc.delete(object)
              }
  
              do {
                  try moc.save()
                  print("saved!")
              } catch let error as NSError  {
                  print("Could not save \(error), \(error.userInfo)")
              } catch {
  
              }
  
  
  
  
          let alert = UIAlertController(title: "Alert", message: "Removed from your Favourite list", preferredStyle: UIAlertControllerStyle.alert)
          alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
          self.present(alert, animated: true, completion: nil)
        //  self.favtable.tableView.reloadData()
  
  
      default: break
      }
  }
  </code></pre>
  
  <p>I dont know what I need to fix here that only deletes the specific record.</p>
  
  <p>Thanks for the time and effort.</p>
  
  <p>Screen 1, when i mark the profile as favourite:</p>
  
  <p><a href="https://i.stack.imgur.com/jjI6Ml.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jjI6Ml.png" alt="enter image description here"></a></p>
  
  <p>Screen 2 when i mark same profile as not favourite :</p>
  
  <p><a href="https://i.stack.imgur.com/R0x2Ql.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R0x2Ql.png" alt="enter image description here"></a></p>
  
  <p>screen 3, when i restart the app again:</p>
  
  <p><a href="https://i.stack.imgur.com/5UFvml.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5UFvml.png" alt="enter image description here"></a></p>
  
  <p>I tried to mark the 2nd profile as not favourite, it does not update the tableview. and when i restart the app, it shows "Label"</p>