【发布时间】:2018-11-26 23:47:58
【问题描述】:
所以我有一个使用 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 获取密钥,因为我的应用是基于位置的。抓住这些钥匙后,我会提取相关信息。我很肯定我无法改变 geofire 存储东西的方式。所以我留下了抓住钥匙并返回快照并根据当前日期创建事件对象。所以基本上如果事件结束日期晚了,那么今天就返回它。如果没有就不要
我有这个 eventsbylocation 节点,它使用 geofire 获取基于位置的键。假设我在一个事件键中的 BEVT 附近,geoFire 会抓住那个键。现在,事件位置节点不包含日期。通过 geofire 获取密钥后,我仅根据从 geofire 收到的密钥查询事件节点。我将如何根据此键集合进行查询,并仅根据该时间戳返回某个日期之后的键。
"eventsbylocation" : {
"ABP" : {
".priority" : "dr4e3nzh0q",
"g" : "dr4e3nzh0q",
"l" : [ 39.9840919, -75.1808035 ]
},
"BEVT" : {
".priority" : "dr4e0r56u7",
"g" : "dr4e0r56u7",
"l" : [ 39.9412882, -75.21873459999999 ]
}
这是通过geoFire抓取钥匙的功能
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)
})
})
}
【问题讨论】:
-
我为什么点赞
-
如果您想获得多个事件,您应该首先在“事件”下添加更高级别的查询。我不太确定如何通过孩子的孩子进行查询,因此您必须自己找出答案,或者更改您的数据结构。
-
@AndréKool Deep Path Query
标签: ios swift firebase firebase-realtime-database