Adding Restores Everything

I have delete functionality in my app for favourites but when I delete a row but add another a favourite that row I just deleted is just added back to the top (gotta make sure that does not happen). I think this has to do with the notification.

class FavouriteManager {
   
   
  static let shared = FavouriteManager()
   
  //it's an array now
  var favArr : [CurrentPlayers] = []
   
  var noRepFav : [CurrentPlayers] = []
   
   
  func add(_ player: CurrentPlayers) {
    favArr.append(player)
    for player in favArr {
      if !noRepFav.contains(player) {
        noRepFav.append(player)
      }
    }
    NotificationCenter.default.post(
      name: .passFavNotification,
      object: player
    )
  }
}
class FavouritesVC: UITableViewController {

   var prefArr: Array<CurrentPlayers> {
    get { FavouriteManager.shared.noRepFav }
    set { FavouriteManager.shared.noRepFav = newValue }
  }

   @objc
    func handleFavNotification(notification: Notification) {
      tableView.reloadData()
    }

   //delete function
   override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  if editingStyle == .delete {
    tableView.beginUpdates()
    prefArr.remove(at: indexPath.row)
    tableView.deleteRows(at: [indexPath], with: .fade)
    tableView.endUpdates()
   }
   }
}
Answered by ForumsContributor in

You should show how you handle the passFavNotification (I mean, where do you call handleFavNotification)

Having two separate properties favArr and noRepFav may be causing the issue.

You should show how you handle the passFavNotification (I mean, where do you call handleFavNotification) 

I will show you where I called it's a detailed tableview controller where the button is to add favourites.

extension Notification.Name {
  static let passFavNotification = Notification.Name("pass")
}


class HockeyDetailVC: UITableViewController {

   override func viewDidLoad() {
    super.viewDidLoad()
    gonnaLoadView()
    tableV.bounces = false
    tableV.alwaysBounceVertical = false
    favButton.layer.cornerRadius = 10
     
    // Add self as Observer and a function to handle it
    NotificationCenter.default.addObserver(self,
    selector: #selector(handleFavNotification(notification:)),
    name: .passFavNotification,
    object: nil)
  }
   
  @objc func handleFavNotification(notification:Notification){
      if let theFav = notification.object as? CurrentPlayers {
        self.item = theFav
      }
    }
}

Having two separate properties favArr and noRepFav may be causing the issue. 

The thing is I need noRepFav because I don't want duplicates in my array.

Accepted Answer

The thing is I need noRepFav because I don't want duplicates in my array.

There are very simple ways to achieve this with a single array. Before appending to this array, check if the item already exist.

I'm confused with this code: you have at least 2 different TableViewControllers (FavouritesVC, HockeyDetailVC). Both of them have handleFavNotification. It is not clear where you delete, where you add, where it is added back.

I'm confused with this code: you have at least 2 different TableViewControllers (FavouritesVC, HockeyDetailVC). Both of them have handleFavNotification. It is not clear where you delete, where you add, where it is added back.

The favourites I want to add is originally in hockeydetailvc where it is added by a button in that vc. I have an observer and notification function there. It is added to an array in FavouriteManager.swift. In my favouritesvc I use that array to hold my favourites and when a new favourites is added to favouritesvc it reloads the tableview with a notification. I can delete a favourite but when I add a new one it's as if the favourite I deleted comes back onto the array.

Adding Restores Everything
 
 
Q