ios - Cycling through value event types and making value UIButton's titleLabel with Firebase in swift -
inside of app have seem have ran error. not runtime error or bug, not being able figure out how or go it. in 1 part of app, users can post data firebase using firebase reference in swift. in part, getting stuck, how not read data, organize that. have list of ten uibuttons on view controller , trying have every time value changed using firebase's method .observeeventtype(.value, withblock: { snapshot in })
catch snapshot.value
, downcast string using as! string
, make that, recent data, first uibutton's title , second uibutton's title old first unbutton's title etc. until ninth uibutton's title tenth , tenth ignored. cycle of uibutton's title based on ten recent posts firebase. great , hope helps other people! thanks! below attempt @ code:
override func viewdidload() { super.viewdidload() // firebase reference defined // uibuttons deck1 - deck10 defined refname.observeeventtype(.value, withblock: { snapshot in // happens first uibutton's title updated new data ( snapshot.value as! string ) self.deck10.titlelabel!.text! = self.deck9.titlelabel!.text! // backwards attempt values not overlap self.deck9.titlelabel!.text! = self.deck8.titlelabel!.text! self.deck8.titlelabel!.text! = self.deck7.titlelabel!.text! self.deck7.titlelabel!.text! = self.deck6.titlelabel!.text! self.deck6.titlelabel!.text! = self.deck5.titlelabel!.text! self.deck5.titlelabel!.text! = self.deck4.titlelabel!.text! self.deck4.titlelabel!.text! = self.deck3.titlelabel!.text! self.deck3.titlelabel!.text! = self.deck2.titlelabel!.text! self.deck2.titlelabel!.text! = self.deck1.titlelabel!.text! self.deck1.titlelabel!.text! = snapshot.value as! string }) }
by way, users posting strings firebase safe might best use snapshot.value as? string
check if string or otherwise return nil.
i think after...
first thing want load last 10 (most current) values firebase array. array used populate , maintain button titles. want keep reference buttons in array well. of off top of head don't copy paste!
var titlearray = [string]() var buttonarray = [nsbutton]() //populate 10 button references titlesref.querylimitedtolast(10).observesingleeventoftype(.value, withblock : { snapshot in child in snapshot.children { let title = child.value["title"] as! string self.titlearray.append(title) } //data ascending, newest @ bottom, reverse self.titlearray = self.titlearray.reverse() index in 0...9 self.buttonarray[index].title = self.titlearray[index] as! string } })
then add observer node notify of current title added.
titlesref.querylimitedtolast(1).observeeventtype(.childadded, withblock : { snapshot in let title = snapshot.value["title"] as! string //always add current @ 'top' of array titlearray(title, atindex: 0) //remove oldest index titlearray.removeatindex(titlearray.count) //iterate on buttonsarray , update titles })
the tricky bit firebase .value in ascending order, meaning oldest title first read in , newest last. use .reverse() reverse ordering of titlesarray newest title @ top.
from on observing newest title added firebase, reading in , inserting @ top of array, refreshing button titles, popping off last (oldest).
another option is: instead of observing last title added , inserting it, could
titlesref.querylimitedtolast(10).observeeventtype(.value...
which, when last 10 change read them in. iterate on (reverse!) , update buttons. might less code it's reading in more data.
edit: may better option. here's code
titlesref.querylimitedtolast(10).observeeventtype(.value, withblock : { snapshot in child in snapshot.children { let title = child.value["title"] as! string titlearray.append(title) } titlearray = titlearray.reverse() index in 0...9 { let title = titlearray[index] self.buttonarray[index].title = titlearray[index] } })
Comments
Post a Comment