Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. @IBAction func saveFav(_ sender: UIButton) {
  2.  
  3. let propertyToCheck = sender.currentTitle!
  4. var proID = saved_id
  5. let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
  6. let task = FavProfile(context: context)
  7.  
  8. switch propertyToCheck {
  9. case "Add to Favourite":
  10. // Link Task & Context
  11. task.busName = bussinessName
  12. task.profileID = Int32(id!)!
  13. print ("saved id is: - (task.profileID)")
  14. print ("saved profile name is: - (task.busName)")
  15. fav_remove_fav_button_label.setTitle("Remove From Favourite", for: .normal)
  16.  
  17. // Save the data to coredata
  18. (UIApplication.shared.delegate as! AppDelegate).saveContext()
  19.  
  20. let _ = navigationController?.popViewController(animated: true)
  21.  
  22. let alert = UIAlertController(title: "Alert", message: "Added to your Favourite list", preferredStyle: UIAlertControllerStyle.alert)
  23. alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
  24. self.present(alert, animated: true, completion: nil)
  25.  
  26. case "Remove from Favourite":
  27.  
  28.  
  29.  
  30. let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "FavProfile")
  31. let moc = getContext()
  32. let result = try? moc.fetch(fetchRequest)
  33. let resultData = result as! [FavProfile]
  34.  
  35. for object in resultData {
  36. moc.delete(object)
  37. }
  38.  
  39. do {
  40. try moc.save()
  41. print("saved!")
  42. } catch let error as NSError {
  43. print("Could not save (error), (error.userInfo)")
  44. } catch {
  45.  
  46. }
  47.  
  48.  
  49.  
  50.  
  51. let alert = UIAlertController(title: "Alert", message: "Removed from your Favourite list", preferredStyle: UIAlertControllerStyle.alert)
  52. alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
  53. self.present(alert, animated: true, completion: nil)
  54. // self.favtable.tableView.reloadData()
  55.  
  56.  
  57. default: break
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement