swift - Update tableview instead of entire reload when navigating back to tableview View Controller -
i have home uiviewcontroller
contains uitableview
. on view controller display games current user reading data firebase in viewwillappear
. view controller user can press button start new game , button takes them next view controller select settings, updates data in firebase , adds new child. once navigate home view controller there anyway update data new child added instead of loading games table view again doing?
this 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 child in snapshot.children { let game = child as! datasnapshot self.games.append(game) self.tableview.reloaddata() } }) } }
i think can use observesingleevent
, .childadded
you can loading of data in viewdidload
, of single child in viewwillappear
since viewdidload
called once initially
since both methods called initially, can have bool flag can control code runs , not , since viewwillappear
called after viewdidload
change value of flag in viewwillappear
method , control execution of code inside viewwillappear
using flag
class somevc: uiviewcontroller { var flag = false override func viewwillappear(_ animated: bool) { super.viewwillappear(animated) if flag { //do work here }else { flag = true } } }
edited: solution can dont in viewdidload
, work in viewwillappear
since in particular scenario data in both calls related (fetching data firebase)
class somevc: uiviewcontroller { var flag = false override func viewwillappear(_ animated: bool) { super.viewwillappear(animated) if flag { //fetch 1 child }else { //fetch data flag = true } } }
Comments
Post a Comment