如何将 observeDataEventType.value 的 firebase 快照转换为 observeSingleEvent?
How do you convert a firebase snapshot of observeDataEventType.value to observeSingleEvent?
我试过的:
我更改了以下内容:
refArtists = Database.database().reference().child("people");
refArtists.observe(DataEventType.value, with: { [weak self]snapshot in
guard let self = self else { return }
if snapshot.childrenCount>0{
self.people.removeAll()
for people in snapshot.children.allObjects as! [DataSnapshot] {
至
refArtists = Database.database().reference().child("people");
refArtists.observeSingleEvent(of: .value, with: { [weak self]snapshot in
guard let self = self else { return }
if snapshot.childrenCount>0{
self.people.removeAll()
for people in snapshot.children.allObjects as! [DataSnapshot] {
另外,我试了一下w/o弱自和守卫。我还添加了:Database.database().isPersistenceEnabled = true //Swift
None 这些在 DataEvent 所做的单元格(图像和文本)中产生的数据。
控制台警告告诉我什么?
控制台中没有错误警告。
印刷品告诉我什么?
peoplekey 仍会打印出来,但在 if 打印后不久就不会打印。
更完整的快照看起来如何?
let thisUsersUid = Auth.auth().currentUser?.uid //Mr. Dunn's uid
refArtists = Database.database().reference().child("people");
refArtists.observeSingleEvent(of: .value, with: { [weak self]snapshot in
if snapshot.childrenCount>0{
self.people.removeAll()
for people in snapshot.children.allObjects as! [DataSnapshot] {
if people.key != thisUsersUid {
print("peoplekey",people.key)
let peopleObject = people.value as? [String: AnyObject]
let peopleEducation = peopleObject?["Education"] as? String
...
let userId = people.key
...
if Calendar.current.isDateInToday(date) {
let distance = locCoord.distance(from: self.dict)
print(distance, "distancexy")
if distance/1609.344 < 3000 && self.array1.contains(people.key){
print(self.array1, "f111111")
print("fee", self.dict )
print(distance, "distancexy")
let peopl = Userx(Education: peopleEducation, .......)
self.people.append(peopl)
let d = people.key
self.printPersonInfo(uid:d)
} else {
print ("w")
}
} else {
print ("alpha")
}
}
print("aaaaaaaa", self.people.map {[=13=].distance})
}
self.people.sort { ([=13=].distance ?? 0) < (.distance ?? 0) }
}
})
注意:我在最初的问题之后添加了以下内容。这似乎是单个事件的问题所在(使用 self.array1)。
guard let myUid = Auth.auth().currentUser?.uid else { return }
refArtists = Database.database().reference().child("people").child(myUid).child("e2")
refArtists.observeSingleEvent(of:.value, with: {snapshot in
let myDomain = snapshot.value as? String
self.bSnap = myDomain
print("haaal", self.bSnap)
let peopleRef = Database.database().reference().child("people")
let thisPersonRef = peopleRef.child(myUid).child("e2")
thisPersonRef.observeSingleEvent(of:.value, with: {snapshot in
if snapshot.exists() {
let query = Database.database().reference().child("people").queryOrdered(byChild: "e2").queryEqual(toValue: self.bSnap)
query.observeSingleEvent(of: .value, with: { snapshot in
var allUsers = snapshot.children.allObjects as! [DataSnapshot]
///////end (1) of comment
if let index = allUsers.firstIndex(where: { [=14=].key == myUid } ) {
allUsers.remove(at: index) //remove the current user
} /////end (2) of comment
for userSnap in allUsers {
let name = userSnap.childSnapshot(forPath: "postID").value as? String
print(name, "NNN")
if let unwrappedName = name {
self.array1.append(unwrappedName)
}
}
print(self.array1, "ahah")
})
} else {
print("no")
}
})
})
我想,另一种选择是保留 DataEventType 并将侦听器分离到某处
这个函数
someRef.observeSingleEvent(of: .value, with: { snapshot in
读取节点 someRef
和 return 将其子数据作为快照一次且仅一次,不离开观察者。进一步的更改不会触发事件(闭包中的代码)
这个
someRef.observe(.value, with: { snapshot in
将观察者添加到 someRef 节点,检索其子数据并将观察者留在该节点上。任何类型的未来更改都将导致事件,并且闭包中的代码将传递包含所有子数据的快照。
请注意,这两个函数都需要一个 DataEventType,因此您可以将其缩短为 .value、.childAdded 等。
所以本质上您不需要 'convert' 一个到另一个,因为它们具有不同的功能但 return 相同的数据 - 至少在最初是这样。 .observe 将继续将数据反馈给应用程序,因为它的子数据发生变化,这可能是也可能不是您想要的。
我试过的:
我更改了以下内容:
refArtists = Database.database().reference().child("people");
refArtists.observe(DataEventType.value, with: { [weak self]snapshot in
guard let self = self else { return }
if snapshot.childrenCount>0{
self.people.removeAll()
for people in snapshot.children.allObjects as! [DataSnapshot] {
至
refArtists = Database.database().reference().child("people");
refArtists.observeSingleEvent(of: .value, with: { [weak self]snapshot in
guard let self = self else { return }
if snapshot.childrenCount>0{
self.people.removeAll()
for people in snapshot.children.allObjects as! [DataSnapshot] {
另外,我试了一下w/o弱自和守卫。我还添加了:Database.database().isPersistenceEnabled = true //Swift
None 这些在 DataEvent 所做的单元格(图像和文本)中产生的数据。
控制台警告告诉我什么?
控制台中没有错误警告。
印刷品告诉我什么?
peoplekey 仍会打印出来,但在 if 打印后不久就不会打印。
更完整的快照看起来如何?
let thisUsersUid = Auth.auth().currentUser?.uid //Mr. Dunn's uid
refArtists = Database.database().reference().child("people");
refArtists.observeSingleEvent(of: .value, with: { [weak self]snapshot in
if snapshot.childrenCount>0{
self.people.removeAll()
for people in snapshot.children.allObjects as! [DataSnapshot] {
if people.key != thisUsersUid {
print("peoplekey",people.key)
let peopleObject = people.value as? [String: AnyObject]
let peopleEducation = peopleObject?["Education"] as? String
...
let userId = people.key
...
if Calendar.current.isDateInToday(date) {
let distance = locCoord.distance(from: self.dict)
print(distance, "distancexy")
if distance/1609.344 < 3000 && self.array1.contains(people.key){
print(self.array1, "f111111")
print("fee", self.dict )
print(distance, "distancexy")
let peopl = Userx(Education: peopleEducation, .......)
self.people.append(peopl)
let d = people.key
self.printPersonInfo(uid:d)
} else {
print ("w")
}
} else {
print ("alpha")
}
}
print("aaaaaaaa", self.people.map {[=13=].distance})
}
self.people.sort { ([=13=].distance ?? 0) < (.distance ?? 0) }
}
})
注意:我在最初的问题之后添加了以下内容。这似乎是单个事件的问题所在(使用 self.array1)。
guard let myUid = Auth.auth().currentUser?.uid else { return }
refArtists = Database.database().reference().child("people").child(myUid).child("e2")
refArtists.observeSingleEvent(of:.value, with: {snapshot in
let myDomain = snapshot.value as? String
self.bSnap = myDomain
print("haaal", self.bSnap)
let peopleRef = Database.database().reference().child("people")
let thisPersonRef = peopleRef.child(myUid).child("e2")
thisPersonRef.observeSingleEvent(of:.value, with: {snapshot in
if snapshot.exists() {
let query = Database.database().reference().child("people").queryOrdered(byChild: "e2").queryEqual(toValue: self.bSnap)
query.observeSingleEvent(of: .value, with: { snapshot in
var allUsers = snapshot.children.allObjects as! [DataSnapshot]
///////end (1) of comment
if let index = allUsers.firstIndex(where: { [=14=].key == myUid } ) {
allUsers.remove(at: index) //remove the current user
} /////end (2) of comment
for userSnap in allUsers {
let name = userSnap.childSnapshot(forPath: "postID").value as? String
print(name, "NNN")
if let unwrappedName = name {
self.array1.append(unwrappedName)
}
}
print(self.array1, "ahah")
})
} else {
print("no")
}
})
})
我想,另一种选择是保留 DataEventType 并将侦听器分离到某处
这个函数
someRef.observeSingleEvent(of: .value, with: { snapshot in
读取节点 someRef
和 return 将其子数据作为快照一次且仅一次,不离开观察者。进一步的更改不会触发事件(闭包中的代码)
这个
someRef.observe(.value, with: { snapshot in
将观察者添加到 someRef 节点,检索其子数据并将观察者留在该节点上。任何类型的未来更改都将导致事件,并且闭包中的代码将传递包含所有子数据的快照。
请注意,这两个函数都需要一个 DataEventType,因此您可以将其缩短为 .value、.childAdded 等。
所以本质上您不需要 'convert' 一个到另一个,因为它们具有不同的功能但 return 相同的数据 - 至少在最初是这样。 .observe 将继续将数据反馈给应用程序,因为它的子数据发生变化,这可能是也可能不是您想要的。