gpt4 book ai didi

ios - 难以理解数组追加

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

func EmptySearchBarList(){

self.PersonalSearchesList = []

currentUserFirebaseReference.child("rooms").observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in


if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {

print("snap")
print(self.PersonalSearchesList.count) // Calling 0 every single time

DataService.ds.REF_INTERESTS.child(interestKey).observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in

if snapshot.value != nil {

if let users = (snapshot.value! as? NSDictionary)?["users"] as? Dictionary<String,AnyObject> {

DataService.ds.REF_USERS.child(topUser).child("pictureURL").observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in

self.PersonalSearchesList.append(eachSearch)
print("first one")
print(self.PersonalSearchesList.count)
})
}
}
})

}

print("Second one")
print(self.PersonalSearchesList.count)

print("Calling to set my sorted PersonalSearchesList")
self.mySortedList = self.PersonalSearchesList.sorted{ $0.users > $1.users }

}
}
initialLoadOver = true
}

The print logs

我试图最终运行的代码是这样的:

 var mySortedList = [Interest](){
didSet{
print("this was called")
let myCount = mySortedList.count
print(self.PersonalSearchesList.count)
print(myCount)

self.tableView.reloadData()
}
}

尝试加载我的 PersonalSearchesList 数组,一旦快照中的快照运行完毕,我将 MySortedList 设置为等于 PersonalSearchesList 并重新加载 TableView 。

我不明白的是为什么打印品会像现在这样打印出来。 snap/0 来 self 的 snap in snapshots 的顶部。它似乎应该改为 snap/1 , snap/2 , snap/3 。

快照完成后要调用的代码在时间轴中是正确的,一旦快照完成该代码就会运行。没有意义的是为什么直到项目实际附加到 PersonalSearchesList 之后才这样做。由于一切都是如此,我将我的过滤数组设置为一个空的个人搜索数组,然后我将其填充。

这里有什么想法吗?

编辑:

 var dispatchGroup = DispatchGroup()
dispatchGroup.enter()
dispatchGroup.leave()

dispatchGroup.notify(queue: DispatchQueue.global(), execute: {

})

最佳答案

DataService.ds.REF_INTERESTS.child(interestKey).observeSingleEvent 正在异步运行,因此所有这些回调(您实际填写列表的地方)都将在从 DataService 调用时运行。

你可以使用一个调度组来做你想做的事。 http://commandshift.co.uk/blog/2014/03/19/using-dispatch-groups-to-wait-for-multiple-web-services/

func EmptySearchBarList(){
var dispatchGroup = DispatchGroup()
self.PersonalSearchesList = []


currentUserFirebaseReference.child("rooms").observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in


if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {
dispatchGroup.enter()

单次

            DataService.ds.REF_INTERESTS.child(interestKey).observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in

if snapshot.value != nil {

if let users = (snapshot.value! as? NSDictionary)?["users"] as? Dictionary<String,AnyObject> {

DataService.ds.REF_USERS.child(topUser).child("pictureURL").observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in

self.PersonalSearchesList.append(eachSearch)
print("snap")
print(self.PersonalSearchesList.count)
})
}
}
dispatchGroup.leave()
})

}

dispatchGroup.notify(queue: DispatchQueue.global(), execute: {
print("Second one")
print(self.PersonalSearchesList.count)

print("Calling to set my sorted PersonalSearchesList")
self.mySortedList = self.PersonalSearchesList.sorted{ $0.users > $1.users }
})


}
}
initialLoadOver = true
}

我不完全确定你可以从同一个线程多次进入和离开组所以你可能必须将代码从进入包装到另一个线程中的回调结束并将其称为异步

关于ios - 难以理解数组追加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39542667/

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