0

I have a UITableView that gets populated by the following firebase database:

"games" : {
    "user1" : {
      "game1" : {
        "currentGame" : "yes",
        "gameType" : "Solo",
        "opponent" : "Computer"
      }
    }
  }

I load all the games in viewDidLoad, a user can create a new game in another UIViewController, once a user does that and navigates back to the UITableView I want to update the table with the new row. I am trying to do that with the following code:

var firstTimeLoad : Bool = true
override func viewDidLoad() {
    super.viewDidLoad()
    if let currentUserID = Auth.auth().currentUser?.uid {
        let gamesRef = 
        Database.database().reference().child("games").child(currentUserID) 

        gamesRef.observe(.childAdded, with: { (snapshot) in
            let game = snapshot
            self.games.append(game)
            self.tableView.reloadData()
        })
    }
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if firstTimeLoad {
        firstTimeLoad = false
    } else {
        if let currentUserID = Auth.auth().currentUser?.uid {
            let gamesRef = Database.database().reference().child("games").child(currentUserID)
            gamesRef.observe(.childAdded, with: { (snapshot) in
                self.games.append(snapshot)
                self.tableView.reloadData()
            })
        }
    }
}

Lets say there is one current game in the data base, when viewDidLoad is run the table displays correctly with one row. However anytime I navigate to another view and navigate back, viewDidAppear is run and for some reason a duplicate game seems to be appended to the games even though no child is added.

The cells are being populated by the games array:

internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
IndexPath) -> UITableViewCell {

    let cell = Bundle.main.loadNibNamed("GameTableViewCell", owner: 
    self, options: nil)?.first as! GameTableViewCell

    let game = games[indexPath.row]
    if let gameDict = game.value as? NSDictionary {
        cell.OpponentName.text = gameDict["opponent"] as? String
    }

    return cell
}

UPDATE:

Thanks to everyone for their answers! It seems like I misunderstood how firebase .childAdded was functioning and I appreciate all your answers trying to help me I think the easiest thing for my app would be to just pull all the data every time the view appears.

7
  • While you append self.games array at that time you have to check that self.games array contains same object as like in response. If yes then you should avoid to append else continue to append. Then perform tableview.reloadData(). Commented Sep 14, 2017 at 3:55
  • But why is .childAdded returning something if no child was added?
    – mikew
    Commented Sep 14, 2017 at 3:56
  • I think you have written same logic in viewDIdLoad and viewWillAppear so both will call at the same time and both response get appended in the same array self.games even if response object is same. Commented Sep 14, 2017 at 3:58
  • I only run viewWillAppear after the initial load so that they are not run at the same time. Thats the boolean check I have in there.
    – mikew
    Commented Sep 14, 2017 at 3:59
  • 1
    Sorry I forgot to include the initialization of firstTimeLoad in my question but it is initially set to to true so that the first viewWillAppear will not be run.
    – mikew
    Commented Sep 14, 2017 at 4:03

3 Answers 3

1

From what I can see, the problem here is that every time you push the view controller and go back to the previous one, it creates a new observer and you end up having many observers running at the same time, which is why your data appears to be duplicated.

What you need to do is inside your viewDidDisappear method, add a removeAllObservers to your gameRef like so :

override func viewDidDisappear(_ animated: Bool) {
   super.viewDidDisappear(animated)

   guard let currentUserId = Auth.auth().currentUser?.uid else {
      return
   }

   let gamesRef = Database.database().reference().child("games").child(currentUserId)

   gamesRef.removeAllObservers()

}

I cannot see all your code here so I am not sure what is happening, but before adding your child added observer, you need to remove all the elements from your array like so :

games.removeAll()

Actually, as per best practices, you should not call your method inside your ViewDidLoad, but instead you should add your observer inside the viewWillAppear method.

I cannot test your code right now but hopefully it should work like that! Let me know if it doesn't :)

UPDATE:

If you want to initially load all the data, and then pull only the new fresh data that is coming, you could use a combination of the observeSingleEvent(of: .value) and observe(.childAdded) observers like so :

var didFirstLoad = false

gamesRef.child(currentUserId).observe(.childAdded, with: { (snapshot) in

    if didFirstLoad {
       // add your object to the games array here
    }

}

gamesRef.child(currentUserId).observeSingleEvent(of: .value, with: { (snapshot) in

    // add the initial data to your games array here

    didFirstLoad = true
}

By doing so, the first time it loads the data, .childAdded will not be called because at that time didFirstLoad will be set to false. It will be called only after .observeSingleEvent got called, which is, by its nature, called only once.

5
  • The reason I had an observer in ViewDidLoad was so that initially all the data would be loaded to the table. I wanted the table to just update every time a user added a new game in the viewWillAppear. I guess I could just have it fetch the data everytime in viewWillAppear its just that it would have to fetch all the data everytime.
    – mikew
    Commented Sep 14, 2017 at 4:07
  • Oh I see ! I edited my answer. Well in that case try removing all the elements from your array before adding the observer and let me know if it works :)
    – user8606263
    Commented Sep 14, 2017 at 4:10
  • Also, if you want to keep the benefits of having all your data loaded during the first load, instead of calling two observers (one in viewDidAppear and one in viewDidLoad), you could just add an if else statement inside your viewDidAppear observer to do that check, because childAdded will be triggered once for each initial child and again every time a new child is added
    – user8606263
    Commented Sep 14, 2017 at 4:14
  • I'm not sure if I understand this correctly but it seems that .childAdded still pulls all the data anyways so I really am getting no increase in efficiency like I intended by just pulling in the new child as this is not how .childAdded functions. Is that correct?
    – mikew
    Commented Sep 14, 2017 at 4:15
  • The first time it would be triggered the first time for each initial child yes. What I would in that situation, is using a observeSingleEvent observer first, I updated my answer for you.
    – user8606263
    Commented Sep 14, 2017 at 4:22
1

Try following code and no need to check for bool , Avoid using bool here its all async methods , it created me an issue in between of my chat app when its database grows

//Remove ref in didLoad
//Remove datasource and delegate from your storyboard and assign it in code so tableView donates for data till your array don't contain any data
//create a global ref 
let gamesRef = Database.database().reference()

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

        self.games.removeAllObjects()
        if let currentUserID = Auth.auth().currentUser?.uid {
            gamesRef.child("games").child(currentUserID)observe(.childAdded, with: { (snapshot) in
                self.games.append(snapshot)
                self.tableView.dataSource = self
                self.tableView.delegate = self
                self.tableView.reloadData()
        })
            gamesRef.removeAllObserver() //will remove ref in disappear itself 
            //or you can use this linen DidDisappear as per requirement 
        }
        else{
           //Control if data not found
            }
        }

//TableView Delegate

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            if self.games.count == 0{
                let emptyLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height))
                emptyLabel.text = "No Data found yet"
                emptyLabel.textAlignment = .center
                self.tableView.backgroundView = emptyLabel
                self.tableView.separatorStyle = .none
                return 0
            }
            else{
                return self.games.count
            }
        }
7
  • I'm not sure if I understand this correctly but it seems that .childAdded still pulls all the data anyways so I really am getting no increase in efficiency like I intended by just pulling in the new child as this is not how .childAdded functions. Is that correct?
    – mikew
    Commented Sep 14, 2017 at 4:13
  • firebase is actually real time update if there is any change in any value of any nod its handler come in action here at this point you need to manage your function to deal with real time update you need to put check in code that in any case similar values don't get append again for this either you can make use of comparator or second clear you arrays before handler execution . in you code as per I checked you are just appending values in array not clearing it this is why maybe multiple same values get append again and again
    – iOS Geek
    Commented Sep 14, 2017 at 4:17
  • just remove all elements before observer as I had mentioned in my answer it will solve your issue. if still you face an issue ask me I will clear it with explanation to you.
    – iOS Geek
    Commented Sep 14, 2017 at 4:18
  • I placed the remove all before the observer but the issue with that is my table is being populated by this array so when they are all removed it then tries to use that array to build the table and now the array is empty and I get an index out of range error.
    – mikew
    Commented Sep 14, 2017 at 4:20
  • have a look again I edited my answer and also remove delegate and datasource from storyboard assign in code as I did
    – iOS Geek
    Commented Sep 14, 2017 at 4:22
0

observe(.childAdded) is called at first once for each existing child, then one time for each child added. Since i also encounter a similar issue, assuming you don't want to display duplicate objects, in my opinion the best approach, which is still not listed in the answers up above, is to add an unique id to every object in the database, then, for each object retrieved by the observe(.childAdded) method, check if the array which contains all objects already contains one with that same id. If it already exists in the array, no need to append it and reload the TableView. Of course observe(.childAdded) must also be moved from viewDidLoad() to viewWillAppear(), where it belongs, and the observer must be removed in viewDidDisappear. To check if the array already includes that particular object retrieved, after casting snapshot you can use method yourArray.contains(where: {($0.id == retrievedObject.id)}).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.