0

I have an array in Firebase composed of 1's and 0's (true and false basically), they are all stored as separate key/value pairs. I would like to retrieve them from Firebase and append each value to an array in Swift.

I have tried this (found elsewhere on SO) but it is not working.

let ref = Firebase(url:"https://<<unique>>.firebaseio.com/users/\(self.UUID)/scoreArray")
    ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
        if snapshot.value is NSNull {
            print("snap is null")
        } else {
            let enumerator = snapshot.children
            while let rest = enumerator.nextObject() as? FDataSnapshot {
                self.scoreArray.append(rest.value! as! String)
            }
        }
    })

It doesn't crash, it just doesn't fill the array, even though if I print(rest.value) it will give me the array.

So I guess the question is, how do I convert rest.value into a usable form?

EDIT Firebase structure as 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>>
Community
  • 1
  • 1
Craig.Pearce
  • 746
  • 7
  • 25
  • Can you please post your Firebase structure (as text please, no images. Firebase Console->Three dots on right->Export JSON). Also, arrays (in Firebase) are inherently difficult to work with as you cannot insert, edit or access the individual elements, so there may be a better structure that will simplify the solution. – Jay Jun 11 '16 at 12:28
  • @Shubhank The difference is the variable key. Those examples the key remains the same so iterating is a bit easy. My key changes to represent the question number. – Craig.Pearce Jun 11 '16 at 12:58
  • the key is diff in them you should try those answers. @Jay he has edited his question for you to take a look on. – Shubhank Jun 11 '16 at 14:11

1 Answers1

1

Working with Array's in Firebase is challenging and in general there are better options to structure data.

In this use case, it may work so here's how it's done.

Given a structure similar to yours:

quiz_0
  quiz_name: The Planets
  scores
     0: Mercury
     1: Venus
     2: Earth

here's how you would read in the data

    let quizzesRef = self.myRootRef.childByAppendingPath("quizzes")

    quizzesRef.observeEventType(.Value, withBlock: { snapshot in

        for 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 is an array
            let answer0 = scores[0] //just to demonstrate accessing the array
            print(answer0)
        }
    })

and the output

Planets
(
    Mercury,
    Venus,
    Earth
)
Mercury

That being said, don't use arrays. Here's a suggestion that may be a far more flexible. Renumbering questions is a snap, modifying the question or answer is easy as well. The -Asidijaid keys are generated using childByAutoId - helps to disassociate dynamic data from static keys.

quiz_0
  quiz_name: The planets
  quiz_data
    -Asok99idkasdsl
       question_number: 0
       question: Which planet closet to the Sun?
       answer: Mercury
    -Yklaosokjdinoisd
       question_number: 1
       question: Which rocky planet is hotter than Mercury
       answer: Venus
    -Klkooksow999sdd
       question_number: 2
       question: What is the third planet from the Sun
       answer: Earth
Jay
  • 34,438
  • 18
  • 52
  • 81