swift - Retrieving an array from Firebase -
this question has answer here:
- how retrieve objects firebase key value 2 answers
i have array in firebase composed of 1's , 0's (true , false basically), stored separate key/value pairs. retrieve them firebase , append each value array in swift.
i have tried (found elsewhere on so) not working.
let ref = firebase(url:"https://<<unique>>.firebaseio.com/users/\(self.uuid)/scorearray") ref.observesingleeventoftype(.value, withblock: { snapshot in if snapshot.value nsnull { print("snap null") } else { let enumerator = snapshot.children while let rest = enumerator.nextobject() as? fdatasnapshot { self.scorearray.append(rest.value! as! string) } } })
it doesn't crash, doesn't fill array, though if print(rest.value)
give me array.
so guess question is, how convert rest.value
usable form?
edit firebase structure requested.
66ec8ac4-<<rest of uuid>> creation_date: "jun 10, 2016" extra_quiz_1 q1: "a" q10: "b" <<continues>> scorearray 0: "0" 1: "1" 2: "0" 3: "0" <<continues>>
working array's in firebase challenging , in general there better options structure data.
in use case, may work here's how it's done.
given structure similar yours:
quiz_0 quiz_name: planets scores 0: mercury 1: venus 2: earth
here's how read in data
let quizzesref = self.myrootref.childbyappendingpath("quizzes") quizzesref.observeeventtype(.value, withblock: { snapshot in child in snapshot.children { let quiz_name = child.value["quiz_name"] as! string print(quiz_name) let scores = child.value["scores"] as! nsarray print(scores) //scores array let answer0 = scores[0] //just demonstrate accessing array print(answer0) } })
and output
planets ( mercury, venus, earth ) mercury
that being said, don't use arrays. here's suggestion may far more flexible. renumbering questions snap, modifying question or answer easy well. -asidijaid keys generated using childbyautoid - helps disassociate dynamic data static keys.
quiz_0 quiz_name: planets quiz_data -asok99idkasdsl question_number: 0 question: planet closet sun? answer: mercury -yklaosokjdinoisd question_number: 1 question: rocky planet hotter mercury answer: venus -klkooksow999sdd question_number: 2 question: third planet sun answer: earth
Comments
Post a Comment