ios - printing only a array with all data with firebase -
printed result
["data1"] ["data1", "data2"] ["data1", "data2", "data3"] ...
wanted result last printed array data or getting last array me out
["data1", "data2", "data3"]
here code use data firebase
var dataarray = [string]() func fetchfirebasedata(){ var datref: databasereference! datref = database.database().reference() datref.child("data").observe(.childadded, with: { (snapshot) in if let dictionary = snapshot.value as? [string: anyobject]{ let data = speisekarteinfos(dictionary: dictionary) data.setvaluesforkeys(dictionary) self.dataarray.append(data.name!) print(self.dataarray) } }, withcancel: nil) }
you observing .childadded
events, called repeatedly, once each entry in database. haven't used firebase, based on little googling sounds should instead observe .value
event, , refactor code expect of them @ once. this:
var dataarray: [string]? func fetchfirebasedata(){ dataarray = [] var datref: databasereference! datref = database.database().reference() datref.child("data").observe(.value, with: { (snapshot) in item in snapshot.children { if let dictionary = item as? [string: anyobject]{ let data = speisekarteinfos(dictionary: dictionary) data.setvaluesforkeys(dictionary) self.dataarray.append(data.name!) } } print(self.dataarray) }, withcancel: nil) }
i'm doing guesswork on format of data .value call, i'm not positive above work, should @ least point in right direction.
note in version request @ once .value observer, loop through children returned, build them array, , print array once they're returned.
from i'm reading looks .value
observer called elements every time database updated. if database dynamic not approach.
Comments
Post a Comment