Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @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 & 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<NSFetchRequestResult>(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
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement