0

I have a home UIViewController that contains a UITableView. On this view controller I display all games for the current user by reading the data from firebase in ViewWillAppear. From this view controller a user can press a button to start a new game and this button takes them to the next view controller to select settings, this then updates the data in firebase and adds a new child. Once they navigate back to the home view controller is there anyway to just update the data with the new child added instead of loading all the games for the table view again as I am currently doing?

This is my current code:

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

    if let currentUserID = Auth.auth().currentUser?.uid {
        let gamesRef = Database.database().reference().child("games").child(currentUserID)
        self.games = []

        gamesRef.observeSingleEvent(of: .value, with: { snapshot in
            for child in snapshot.children {
                let game = child as! DataSnapshot
                self.games.append(game)
                self.tableView.reloadData()
            }
        })
    }
}
mikew
  • 347
  • 6
  • 12
  • Can you show some code, I think you can use `observeSingleEvent` and `.childAdded` – 3stud1ant3 Sep 14 '17 at 02:07
  • But the first time this view appears I need to load all the data so they can see all their games when they first start the app. – mikew Sep 14 '17 at 02:08
  • Then I think you can do the loading of all the data in `viewDidLoad` and of single child in `viewWillAppear` since `viewDidLoad` will be called once initially – 3stud1ant3 Sep 14 '17 at 02:10
  • if you want to fetch the games on first load only, transfer your data fetchin in **viewDidLoad()** then try the suggestion of @3stud1ant3 – Ace Rivera Sep 14 '17 at 02:10
  • Ah okay this makes sense, I guess I was confused about how `ViewDidLoad` worked vs how `ViewWillAppear` works. I thought they both were run anytime the view appeared but `ViewWillAppear` was just run first. – mikew Sep 14 '17 at 02:12
  • Note that `viewWillAppear` is called everytime when your viewController is about to be displayed on the screen and `viewDidLoad` is called once initially – 3stud1ant3 Sep 14 '17 at 02:13
  • Sorry for the trivial question I should've done some more research on the difference between the two. Thanks guys! – mikew Sep 14 '17 at 02:14
  • 1
    Learn about ViewController Lifecycle here https://stackoverflow.com/questions/5562938/looking-to-understand-the-ios-uiviewcontroller-lifecycle – 3stud1ant3 Sep 14 '17 at 02:15

1 Answers1

3

I think you can use observeSingleEvent and .childAdded

You can do the loading of all the data in viewDidLoad and of single child in viewWillAppear since viewDidLoad will be called once initially

Since both methods will be called initially, so we can have a bool flag so we can control which code runs initially and which does not , since viewWillAppear is called after viewDidLoad so we change the value of this flag in viewWillAppear method and then control the execution of code inside viewWillAppear using this flag

class SomeVC: UIViewController {

        var flag = false
        override func viewWillAppear(_ animated: Bool) {
                super.viewWillAppear(animated)
                if flag {
                        //do your work here
                }else {
                   flag = true
                }

        }
}

Edited: Another solution can be that you dont do anything in viewDidLoad and do the work only in viewWillAppear since in this particular scenario data in both calls are related (fetching the data from Firebase)

class SomeVC: UIViewController {

            var flag = false
            override func viewWillAppear(_ animated: Bool) {
                    super.viewWillAppear(animated)
                    if flag {
                            //fetch only one child
                    }else {
                            //fetch all the data initially
                            flag = true
                    }

            }
    }
3stud1ant3
  • 3,586
  • 2
  • 12
  • 15
  • Sorry I have one additional question. There seems to be one bug, `viewDidLoad` and `viewWillAppear` are both run the first time the app loads, is there a way inside of `viewWillAppear to know that its the first time and not look for a child added? Currently it is adding duplicates rows since both those are being run the first time. – mikew Sep 14 '17 at 02:49