gpt4 book ai didi

ios - firebase 查询返回错误的快照

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

所以我有一个使用 firebase 的应用程序。在此应用程序中,我想抓取结束时间大于或等于当天的事件。我创建了这个函数,我相信它可以实现这一点。但是,它什么也没返回

static func show(forEventKey eventKey: String, completion: @escaping (Event?) -> Void) {
print(eventKey)
let ref = Database.database().reference().child("events").child(eventKey)

ref.queryOrdered(byChild: "end")
.queryStarting(atValue: Date().timeIntervalSince1970, childKey: "end")
.observeSingleEvent(of: .value, with: { (snapshot) in

print(snapshot)

guard let event = Event(snapshot: snapshot) else {
return completion(nil)
}

completion(event)

})
}

我的树中事件节点下的子项如下所示

"BEVT" : {
"attend:count" : 5,
"event:category" : "Seize The Night",
"event:city" : "Philadelphia",
"event:date" : {
"end:date" : "06/18/2018",
"end:time" : "7:01 PM",
"start:date" : "06/17/2018",
"start:time" : "12:01 PM"
},
"event:datetime" : {
"end" : 1529362860,
"start" : 1529251260
},
"event:description" : "Travis Scott is ready to hit the road. The 24-year-old hitmaker will embark on his Birds Eye View tour, powered by Live Nation, in support of his sophomore album and Billboard 200 No. 1 Birds in the Trap Sing McKnight.",
"event:imageURL" : "https://firebasestorage.googleapis.com/v0/b/eventful-3d558.appspot.com/o/event_flyers%2FtravisScott-compressor.png?alt=media&token=c6e46da1-1395-4a37-a8fc-3ff53d5c0d4d",
"event:name" : "birds eye view tour",
"event:promo" : "https://firebasestorage.googleapis.com/v0/b/eventful-3d558.appspot.com/o/event_promo_vid%2FBEVT%2FTravis%20Scott_%20Houston%20Birds%20Eye%20View%20Tour%20Promo.mp4?alt=media&token=6d27d76e-281e-4083-a0ff-dbe2f25703e7",
"event:state" : "PA",
"event:street:address" : "5748 Baltimore Pike",
"event:zip" : 19143
}

当我打印快照时,它看起来像这样,这表明它找到了它,但它没有返回整个快照

Snap (BEVT) {
"event:datetime" = {
end = 1529445600;
start = 1529442000;
};
}

任何人都可以看到我的查询出了问题吗,因为我有点困惑?

我正在使用 geofire 获取 key ,因为我的应用程序是基于位置的。拿到这些 key 后,我会提取相关信息。我确信我无法改变 geofire 存储东西的方式。因此,我只剩下获取 key 并返回快照并根据当前日期创建事件对象。因此,基本上,如果事件结束日期晚于今天,则将其返回。如果没有就不要

我有这个 eventsbylocation 节点,它使用 geofire 根据位置获取 key 。假设我靠近 BEVT,它位于事件键中,geoFire 将抓取该键。现在事件位置节点不保存日期。通过 geofire 获取 key 后,我仅根据从 geofire 收到的 key 查询事件节点。我将如何基于此键集合进行查询,并仅返回基于该时间戳的特定日期之后的键。

"eventsbylocation" : {
"ABP" : {
".priority" : "dr4e3nzh0q",
"g" : "dr4e3nzh0q",
"l" : [ 39.9840919, -75.1808035 ]
},
"BEVT" : {
".priority" : "dr4e0r56u7",
"g" : "dr4e0r56u7",
"l" : [ 39.9412882, -75.21873459999999 ]
}

这是通过geoFire抓取 key 的函数

 static func showEvent(for currentLocation: CLLocation,completion: @escaping ([Event]) -> Void) {
//getting firebase root directory
var keys = [String]()
var currentEvents = [Event]()
var geoFireRef: DatabaseReference?
var geoFire:GeoFire?
geoFireRef = Database.database().reference().child("eventsbylocation")
geoFire = GeoFire(firebaseRef: geoFireRef!)
let circleQuery = geoFire?.query(at: currentLocation, withRadius: 17.0)
circleQuery?.observe(.keyEntered, with: { (key: String!, location: CLLocation!) in
print("Key '\(key)' entered the search area and is at location '\(location)'")
if let currentKey = key {
keys.append(currentKey)
}
})

circleQuery?.observeReady({
let dispatchGroup = DispatchGroup()
for key in keys {
dispatchGroup.enter()
EventService.show(forEventKey: key, completion: { (event) in
if let currentEvent = event {
currentEvents.append(currentEvent)
}
dispatchGroup.leave()
})
}

dispatchGroup.notify(queue: .main, execute: {
print(currentEvents.count)
completion(currentEvents)
})

})

}

最佳答案

您的代码试图过于具体,并且您告诉它查看查询中数据的特定端 - 这没有意义,就好像您知道确切的路径一样,您不需要查询!

这是一个常见的误解 - 只需记住,查询需要提供父节点,然后提供您要查询的内容的子节点,因为然后它将迭代父节点下方的 child_nodes。

parent_node
child_node
child_node_of_what_you_are_querying
child_node
child_node_of_what_you_are_querying

所以你的查询应该是这样的。

 let ref = Database.database().reference().child("events").child("event_Key")

ref.queryOrdered(byChild: "end")
.queryStarting(atValue: Date().timeIntervalSince1970)
.observeSingleEvent(of: .value, with: { (snapshot) in

print(snapshot)

})

关于ios - firebase 查询返回错误的快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50904249/

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