如何通过 swift ios 中的键值读取子数据或过滤 firebase 数据
How to read child or filter firebase data by its key value in swift ios
我是 swift 和 firebase 的新手,我正在尝试使用下面的代码打印所有商品和价格,我希望能够打印 ..
输出:
- 红椅100
- 沙发床120
var ref = Firebase(url: "https://jeanniefirstapp.firebaseio.com")
var item1 = ["name": "Alan Turning", "item" : "Red Chair", "price": "100"]
var item2 = ["name": "Grace Hopper", "item": "Sofa Bed" , "price": "120"]
var item3 = ["name": "James Cook" , "item": "White Desk", "price": "250"]
var item4 = ["name": "James Cook" , "item": "Mattress Cal King", "price": "100"]
override func viewDidLoad() {
super.viewDidLoad()
var usersRef = ref.childByAppendingPath("users")
var users = ["item1": item1, "item2": item2, "item3" : item3 , "item4" : item4 ]
usersRef.setValue(users)
}
ref.queryOrderedByChild("price").observeEventType(.Value, withBlock: { snapshot in
if let price = snapshot.value["price"] as? Int {
println("\(snapshot.key) price at \(price) Dollars ")
println(snapshot.key)
}
})
因为你想为每个项目执行相同的代码,你会想使用 .ChildAdded
:
ref.queryOrderedByChild("price").observeEventType(.ChildAdded, withBlock: { snapshot in
if let price = snapshot.value["price"] as? Int {
println("\(snapshot.key) price at \(price) Dollars ")
println(snapshot.key)
}
})
有关更多信息和示例,请参阅 page on retrieving data in the Firebase guide for iOS developers。
更新
我最终在本地 xcode 中使用了您的代码,发现有两个问题。所以这三个加起来:
您正在侦听 .Value
事件,但您的块一次处理一个项目。解决方案:
ref.queryOrderedByChild("price")
.observeEventType(.ChildAdded, withBlock: { snapshot in
您正在监听顶级的 .Value
事件,但您正在添加 users
下的项目。解决方案:
ref.childByAppendingPath("users")
.queryOrderedByChild("price")
.observeEventType(.ChildAdded, withBlock: { snapshot in
您正在测试价格是否为 Int
,但将它们添加为字符串。解决方案:
var item1 = ["name": "Alan Turning", "item" : "Red Chair", "price": 100]
var item2 = ["name": "Grace Hopper", "item": "Sofa Bed" , "price": 120]
var item3 = ["name": "James Cook" , "item": "White Desk", "price": 250]
var item4 = ["name": "James Cook" , "item": "Mattress Cal King", "price": 100]
进行这些更改后,代码会为我打印出这些结果:
item1 price at 100 Dollars
item1
item4 price at 100 Dollars
item4
item2 price at 120 Dollars
item2
item3 price at 250 Dollars
item3
我是 swift 和 firebase 的新手,我正在尝试使用下面的代码打印所有商品和价格,我希望能够打印 ..
输出:
- 红椅100
- 沙发床120
var ref = Firebase(url: "https://jeanniefirstapp.firebaseio.com")
var item1 = ["name": "Alan Turning", "item" : "Red Chair", "price": "100"]
var item2 = ["name": "Grace Hopper", "item": "Sofa Bed" , "price": "120"]
var item3 = ["name": "James Cook" , "item": "White Desk", "price": "250"]
var item4 = ["name": "James Cook" , "item": "Mattress Cal King", "price": "100"]
override func viewDidLoad() {
super.viewDidLoad()
var usersRef = ref.childByAppendingPath("users")
var users = ["item1": item1, "item2": item2, "item3" : item3 , "item4" : item4 ]
usersRef.setValue(users)
}
ref.queryOrderedByChild("price").observeEventType(.Value, withBlock: { snapshot in
if let price = snapshot.value["price"] as? Int {
println("\(snapshot.key) price at \(price) Dollars ")
println(snapshot.key)
}
})
因为你想为每个项目执行相同的代码,你会想使用 .ChildAdded
:
ref.queryOrderedByChild("price").observeEventType(.ChildAdded, withBlock: { snapshot in
if let price = snapshot.value["price"] as? Int {
println("\(snapshot.key) price at \(price) Dollars ")
println(snapshot.key)
}
})
有关更多信息和示例,请参阅 page on retrieving data in the Firebase guide for iOS developers。
更新
我最终在本地 xcode 中使用了您的代码,发现有两个问题。所以这三个加起来:
您正在侦听
.Value
事件,但您的块一次处理一个项目。解决方案:ref.queryOrderedByChild("price") .observeEventType(.ChildAdded, withBlock: { snapshot in
您正在监听顶级的
.Value
事件,但您正在添加users
下的项目。解决方案:ref.childByAppendingPath("users") .queryOrderedByChild("price") .observeEventType(.ChildAdded, withBlock: { snapshot in
您正在测试价格是否为
Int
,但将它们添加为字符串。解决方案:var item1 = ["name": "Alan Turning", "item" : "Red Chair", "price": 100] var item2 = ["name": "Grace Hopper", "item": "Sofa Bed" , "price": 120] var item3 = ["name": "James Cook" , "item": "White Desk", "price": 250] var item4 = ["name": "James Cook" , "item": "Mattress Cal King", "price": 100]
进行这些更改后,代码会为我打印出这些结果:
item1 price at 100 Dollars
item1
item4 price at 100 Dollars
item4
item2 price at 120 Dollars
item2
item3 price at 250 Dollars
item3