swift3 - Firebase saving multiple child values to arrays -
looking @ structure of firebase database:
i attempting save values every key every cryptocurrency separate arrays.
for example, 'coinamount' key, array looking ["1.0,"2.0"], , 'coinname' ["bitcoin","ethereum"] etc each of 5 keys.
my attempt @ this:
let index = nsindexpath(item: 0, section: 0) if portfoliocoinfullnamestring.count > 0 { print("portfoliovc: number of coins in portfolio > 0") let coinref = ref.child("\(portfoliocoinfullnamestring[index.row])") coinref.observe(dataeventtype.value) { (snapshot) in child in snapshot.children { let snap = child as! datasnapshot let key = snap.key let value = snap.value print("key = \(key) ,, value = \(value!)") if key == "coinamount" { self.firebasecoinamountarray.append(double(value as! string)!) } else if key == "coinname" { self.firebasecoinnamearary.append(value as! string) } else if key == "coinprice" { self.firebasecoinpricearray.append(double(value as! string)!) } else if key == "coinsymbol" { self.firebasecoinsymbolarray.append(value as! string) } else if key == "cointotalfiat" { self.firebasecointotalfiatarray.append(double(value as! string)!) } else { print("portfoliovc: key not match anything!") } } } } else { print("portfoliovc: number of coins in portfolio !> 0") }
this works adding first coin, when attempt add second 1 2 values appended array contains 3, , if add third coin append 3 values. not sure why looping through each coin?
edit: searched around stackoverflow , came across method:
let index = nsindexpath(item: 0, section: 0) if portfoliocoinfullnamestring.count > 0 { print("portfoliovc: number of coins in portfolio > 0") let coinref = ref.child("\(portfoliocoinfullnamestring[index.row])") coinref.observe(dataeventtype.value) { (snapshot) in let value = snapshot.value as? nsdictionary let coinname = value?["coinname"] as? string ?? "" self.firebasecoinnamearary.append(coinname) print("aaaaaa:\(self.firebasecoinnamearary)") } } else { print("portfoliovc: number of coins in portfolio !> 0") }
but still have issue of when adding coin second element in array, array contains 3x first coin added instead of 2 entires of first coin , second coin. know if statement check if array contains name , if dont add it, how number values theres possibility coins have same price.??
edit2: been thinking it, work if observe values , not changes, save of arrays, populate cells them. if user edits 'coinamount' value in app, updates in firebase, not arrays. then, upon user closing app/logging out, next time portfolio shown re-pull values firebase contains updated numbers?
edit3: also, nodes under portfolio called 'bitcoin' etc, if called them coin1, coin2, coin3 etc, how path , key , values coin3? there way values nodes under 'protfolio' in 1 go?
the idea once save firebase data arrays populate tableview cell of data.
any appreciated :)
thanks,
jeremy
in order don't use for-loop use .childadded
i provide example
database.add entire path.child("portfolio").observe(dataeventtype.childadded) { (snapshot) in let key = snapshot.key let value = snapsnapshot.value as? nsdictionary //add in firebase let coinamount = value?["coinamount"] as? string let cointname = value?["coinname"] as? string //.... added variables database //use variables above , append array , work! } }
Comments
Post a Comment