gpt4 book ai didi

ios - Firebase queryEqual 不起作用

转载 作者:行者123 更新时间:2023-11-30 12:38:07 24 4
gpt4 key购买 nike

我的 Firebase 中有以下层次结构:

firebase hierarchy

func invitedEvents() {
DataService.ds.REF_EVENTS.queryOrdered(byChild: "eventParticipant").observe(.childAdded, with: { (snap) in
print("KEEEY:\(snap.key)")
let value = snap.value as? NSDictionary
if snap.exists() , value?["eventParticipant"] != nil{
print("eventParticipant:\(value?["eventParticipant"])")
var valueArray = value?["eventParticipant"] as! [[String:AnyObject]]
for (index, element) in valueArray.enumerated() {
print("valueArray\(index): \(element)")
//self.selectedContacts.append(element as! Participant)
if element["partPhone"] as! String == "00 11 111 1111" {
print("KEY:\(snap.key) PHONE:\(element["partPhone"])")
self.eventKey = snap.key
print("snap.key:\(snap.key)")
//child("eventItem").child("eventOrganized").queryOrdered(byChild: snap.key).queryEqual(toValue: "1")

DataService.ds.REF_USERS.child("eventItem").child("eventOrganized").child(snap.key).observe(.value, with: { (snapshot) in
print("With Snapshot Key: \(snapshot)")
})
//DataService.ds.REF_USERS.queryOrdered(byChild: "provider").queryEqual(toValue: "email").observe(.value, with: { (snapshot) in
// print("With Provider:\(snapshot.value)")
//})

DataService.ds.REF_EVENTS.child(self.eventKey).observe(.value, with: { (snap) in

print(snap)
if snap.value is NSNull {
print("Event is not found!")
}
else {

let value = snap.value as? NSDictionary
print(value?["eventLocation"] ,
value?["eventCurrency"],
value?["eventDesc"],
value?["eventBeginDate"],
value?["eventEndDate"],
value?["eventCurrency"],
value?["eventLocation"],
value?["eventName"],
value?["eventDesc"],
value?["eventPrice"])

if
let eventAddress = value?["eventLocation"] ,
let eventCurrency = value?["eventCurrency"],
let eventDesc = value?["eventDesc"],
let eventBeginDate = value?["eventBeginDate"],
let eventEndDate = value?["eventEndDate"],
let eventImg = value?["eventCurrency"],
let eventLocation = value?["eventLocation"],
let eventName2 = value?["eventName"],
let eventNote = value?["eventDesc"],
let eventPrice = value?["eventPrice"]
{
let eventDetails = Event(
eventAddress: value?["eventLocation"] as! String,
eventCurrency: value?["eventCurrency"] as! String,
eventDesc: value?["eventDesc"] as! String,
eventBeginDate: value?["eventBeginDate"] as! String,
eventEndDate: value?["eventEndDate"] as! String,
eventImg: value?["eventCurrency"] as! String,
eventLikes: 1,
eventLocation: value?["eventLocation"] as! String,
eventName: value?["eventName"] as! String,
eventNote: value?["eventDesc"] as! String,
eventPrice: value?["eventPrice"] as! String,
eventCreated: value?["eventCreated"] as! String,
eventStatus: 0 as! Int?
)

//print(eventDetails)
let eventName = value?["eventName"] as! String
self.userPastEventNameList.append(eventName)
self.events.append(eventDetails)
print(eventAddress,eventCurrency,eventDesc, eventBeginDate, eventEndDate, eventImg, eventLocation, eventName2, eventNote, eventPrice)
}
else {
print("NULL FOUND")


}

}
DispatchQueue.main.async{
self.tableView.reloadData()
}
self.removeLoadingScreen()
})

//})
}
}


}
})
}

和 snap.keys 在 print("snap.key:(snap.key)"):

snap.key:-KePXAuTADuTLTsZ7qbe
snap.key:-KePjqMKrARQmp-gTss-
snap.key:-KePsCS5rB4lTwy6GWea
snap.key:-KePwdRQPFFX30_GDAOK
snap.key:-KePyNVghX4MkSxI_1fx
snap.key:-KePz200QsJp6CSb3bVN

如果我使用这个,那么我会得到一个快照:

print("snap.key:\(snap.key)")
DataService.ds.REF_USERS.queryOrdered(byChild: "provider").queryEqual(toValue: "email").observe(.value, with: { (snapshot) in
print("With Provider:\(snapshot.value)")})

结果:

With Provider:Optional({
JIbrGLdfCDUpMUOHEu7KzRye8ZK2 = {
provider = email;
};
})

如果我使用这个,那么我不会得到任何结果:

 print("snap.key:\(snap.key)")    
DataService.ds.REF_USERS.child("eventItem").child("eventOrganized").queryOrdered(byChild: snap.key).queryEqual(toValue: 1).observe(.value, with: { (snapshot) in ("With Snapshot Key:\(snapshot.value)")})

结果:

With Snapshot Key:Optional(<null>)

有人可以帮助我吗?我做错了什么?

最佳答案

对,

DataService.ds.REF_USERS.child("eventItem").child("eventOrganized").queryOrdered(byChild: snap.key).queryEqual(toValue: 1).observe(.value, with: { (snapshot) in ("With Snapshot Key:\(snapshot.value)")})

这不起作用,因为您缺少对 V9T3cEgEGPRmlkMQb32hxa5gG7L2 节点的引用。

如果您想要该键下所有值为 1 的 eventOrganized 节点,则需要使用以下查询。

DataService.ds.REF_USERS.child("V9T3cEgEGPRmIkMQb32hxa5gG7L2").child("eventItem").child("eventOrganized").queryOrderedByValue().queryEqual(toValue: 1).observe(.value, with: { (snapshot) in
print("Snapshot Value:\(snapshot.value)")
})

显然你不想直接引用V9T3cEgEGPRmlkMQb32hxa5gG7L2,为了可重用性,你应该将此 key 存储在一个变量中(也许你有,我在你的代码中看不到)。我想这就是你想要的。

关于ios - Firebase queryEqual 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42609735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com