swift - 在 swift/firebase 项目中,当使用 .observe 而不是使用 SingleEvent 时,是什么导致数组可读?
问题描述
因此,当在下面的第一个快照(见粗体)中使用 Single Event 时,第一个快照无法拾取 array1(来自第二个快照)。但是当使用 .observe 时,它可以。为什么会这样?
起初我不明白为什么 Single Even 会失败,但 .observe 不会。我做了很多打印,发现第二张快照效果很好。但如果使用 .SingleEvent,则无法识别第二个快照中的 array1。但是,如果改为使用 .observe,则可以识别。
/////第一个快照
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 {$0.distance})
}
self.people.sort { ($0.distance ?? 0) < ($1.distance ?? 0) }
}
})
//////第二个快照使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: { $0.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")
}
})
})
解决方案
推荐阅读
- algorithm - n 队、m 组、k 前进者和幸运失败者的锦标赛树播种
- php - 根据字段值合并两个数组数组
- c# - Zendesk REST API Url 文章在浏览器中返回数据,但在 Visual Studio 中给出未经授权的错误
- python - Keras - 无法将我的输入数据释放到 keras 模型。检查输入时出错
- python - 在 BigQuery 中仅为一列设置架构
- openstreetmap - HOTOSM 数据与 openstreetmap.org 数据相同
- swift - Safari 扩展无法执行 URLSession 任务并出现错误:路径失败:/var/run/mDNSResponder 不允许操作
- codeigniter - 如何修复“redis会话自动过期”
- powershell - 用于 IIS 条件 URL 重写 matchType 的 Power shell 脚本
- python - 用python中的值填充CSV文件的列