title: Check for Saved IDs in Core Data database and show/hide button “Add to Fav” in Swift 3
body: <p>I have a Profile View Controller (VC1), and on this VC1 I have a button that says "Add to Favourite". When I tap this button, I am able to save this details (ID and Profile Name) in the Core Data database and able to see this data/s in the table list view inside my Fav View Controller (VC2). Now, when i go to the VC2, I can see all the favourites in the table list and when I click on one of the record, it will show me details of that Profile. </p>
  
  <p>But now the problem is, The button which I have here "Add to Favourite", I need to change it <strong>"Remove from Favourite"</strong> as this profile is already marked as favourite.</p>
  
  <p>I am able to understand the concept, that the obj ID needs to check with in the database and if there is a match then the Button.setTitle will change and do the required function. But I am not able to figure this out correctly.</p>
  
  <p>Thanks for your time, really appreciate!!!</p>
  
  <p>My code for saving the data</p>
  
  <pre><code> @IBOutlet weak var fav_remove_fav_button_label: UIButton!
  
  
  @IBAction func saveFav(_ sender: Any) {
  
      var proID = saved_id
      let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
      let task = FavProfile(context: context) // 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)
  
  
  }
  </code></pre>
  
  <p>my function to fetch and check the data</p>
  
  <pre><code>func isExist(id: Int) -&gt; Bool {
      var error: NSError?
      var moc: NSManagedObjectContext!
      var fetchRequest = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: "FavProfile")
      if let results = try! moc.fetch(fetchRequest) as? [FavProfile] {
  
          if !results.isEmpty {
              for x in results {
                  if x.profileID  == Int32(id) {
                      print("already exist")
                      moc.delete(x)
                  }
              }
          }
      } else {
          print(error)
      }
  
      return false
  }
  </code></pre>