从Firebase中检索数组 [英] Retrieving an array from Firebase

查看:111
本文介绍了从Firebase中检索数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firebase中有一个由1和0组成的数组(基本上是true和false),它们都是作为单独的键/值对存储的。我想从Firebase中检索它们,并将每个值附加到Swift中的一个数组中。



我试过这个(在

  let ref = Firebase(url:https://<< unique>> .firebaseio.com / users / \(self.UUID)/ scoreArray)
ref.observeSingleEventOfType(.Value,withBlock:{snapshot在
中,如果snapshot.value是NSNull {
print(snap is null)
} else {
let enumerator = snapshot.children
while let rest = enumerator .nextObject()as?FDataSnapshot {
self.scoreArray.append(rest.value!as!String)
}
}
})
打印(休息。值)
它会给我的数组。所以我想问题是,如何将 rest.value 转换成一个可用的形式?

>

按照要求编辑Firebase结构。

  66EC8AC4-<<< UUID>> 
creation_date:2016年6月10日
extra_quiz_1
q1:a
q10:b
<继续>>
scoreArray
0:0
1:1
2:0
3:0
<<继续> ;>


解决方案

是更好的选择结构数据。



在这个用例,它可能工作,所以这是如何完成的。 >给定一个类似于你的结构:

  quiz_0 
quiz_name:行星
分数
0:Mercury
1:Venus
2:Earth

阅读数据

  let quizzesRef = self.myRootRef.childByAppendingPath(quizzes)

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

for snapshot.children中的子元素{
let quiz_name = child.value [quiz_name] as!String
print quiz_name)
let scores = child.value [scores] as!NSArray
print(scores)// scores是一个数组
let answer0 = scores [0 ] //只是为了演示访问数组
print(answer0)
}
})

和输出

 行星

水星,
金星,
地球

水星

不要使用数组。这个建议可能会更加灵活。对问题进行重新编号很简单,修改问题或答案也很容易。使用childByAutoId生成-Asidijaid键 - 帮助解除静态键的动态数据关联。

  quiz_0 
quiz_name:The行星
quiz_data
-Asok99idkasdsl
question_number:0
问题:哪个星球靠近太阳?
答案:Mercury
-Yklaosokjdinoisd
question_number:1
问题:哪个岩石地球比水星热
答案:金星
-Klkooksow999sdd
问题编号:2
问题:什么是太阳的第三个星球
答案:Earth


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>>

解决方案

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

这篇关于从Firebase中检索数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆