无法理解附加的数组

时间:2016-09-17 03:49:28

标签: ios arrays swift

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来自我的快照快照的顶部。它似乎应该是snap / 1,snap / 2,snap / 3.

完成快照后要调用的代码在时间轴中是正确的,一旦快照完成代码运行。什么是没有意义的是为什么直到那之后项目实际上被附加到PersonalSearchesList。因为一切都是我的生活方式。将我的过滤后的数组设置为空的个人搜索数组,然后我将其填满。

这里有什么想法吗?

编辑:

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

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

        })

1 个答案:

答案 0 :(得分:1)

DataService.ds.REF_INTERESTS.child(interestKey).observeSingleEvent正在异步运行,所以当从DataService调用它们时,所有这些回调(实际填充列表的地方)都会运行。

您可以使用dispatch group做您想做的事。 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
}

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