Ios 如何将完成块添加到.childAdded firebase查询?

Ios 如何将完成块添加到.childAdded firebase查询?,ios,swift,firebase,Ios,Swift,Firebase,我有下面的fetch函数。我如何添加一个完成块,以便在它完成时我可以做些什么 此查询将多次在内部运行代码 func getFollowers() { print("get followers called") let ref = Database.database().reference() ref.child("users2").child((Auth.auth().currentUser?.uid)!).child("Following").observe(.chil

我有下面的fetch函数。我如何添加一个完成块,以便在它完成时我可以做些什么

此查询将多次在内部运行代码

func getFollowers() {
    print("get followers called")
    let ref = Database.database().reference()
    ref.child("users2").child((Auth.auth().currentUser?.uid)!).child("Following").observe(.childAdded) { (snap) in
        let personBeignFollow = snap.key
        self.peopleUserFollows.append(personBeignFollow)
        print("Appened: ", personBeignFollow)
        self.fetchAllUserFirstPostMedia(user: personBeignFollow)
    }
}
我已经看过了,但没能成功

以下是我尝试过的:

    func getFollowers(_: ()-> ()) {
    print("get followers called")
    let ref = Database.database().reference()
    ref.child("users2").child((Auth.auth().currentUser?.uid)!).child("Following").observe(.childAdded) { (snap) in
        let personBeignFollow = snap.key
        self.peopleUserFollows.append(personBeignFollow)
        print("Appened: ", personBeignFollow)
        self.fetchAllUserFirstPostMedia(user: personBeignFollow)
    }
}
那么它被称为:

getFollowers() {
   self.collectionView.reloadData()
}

在函数声明中,可以添加以下内容:

func getFollowers(_ completion: @escaping () -> Void) {
    print("get followers called")
    let ref = Database.database().reference()
    ref.child("users2").child((Auth.auth().currentUser?.uid)!).child("Following").observe(.childAdded) { (snap) in
        let personBeignFollow = snap.key
        self.peopleUserFollows.append(personBeignFollow)
        print("Appened: ", personBeignFollow)
        self.fetchAllUserFirstPostMedia(user: personBeignFollow)

       // tell the calling function to execute the completion handler again
       completion()
    }
}
然后要使用它,您可以执行以下操作:

getFollowers {
 // whatever you want to do after the query has run
}
func getFollowers(_ completion: (String) -> Void) {
    ref.child("users2").child((Auth.auth().currentUser?.uid)!).child("Following").observe(.childAdded) { (snap) in
        let personBeignFollow = snap.key
        print("Appened: ", personBeignFollow)        
       // tell the calling function to execute the completion handler again
       completion(personBeignFollow)
    }
}
这并不直接重要,但作为一种常见的设计实践,最好将从查询中检索到的新数据作为参数传递到完成处理程序中,而不是在类上分配属性

看起来是这样的:

getFollowers {
 // whatever you want to do after the query has run
}
func getFollowers(_ completion: (String) -> Void) {
    ref.child("users2").child((Auth.auth().currentUser?.uid)!).child("Following").observe(.childAdded) { (snap) in
        let personBeignFollow = snap.key
        print("Appened: ", personBeignFollow)        
       // tell the calling function to execute the completion handler again
       completion(personBeignFollow)
    }
}
然后,您的呼叫站点可能如下所示:

getFollowers { newUser in
    self.fetchAllUserFirstPostMedia(user: newUser)
}

在函数声明中,可以添加以下内容:

func getFollowers(_ completion: @escaping () -> Void) {
    print("get followers called")
    let ref = Database.database().reference()
    ref.child("users2").child((Auth.auth().currentUser?.uid)!).child("Following").observe(.childAdded) { (snap) in
        let personBeignFollow = snap.key
        self.peopleUserFollows.append(personBeignFollow)
        print("Appened: ", personBeignFollow)
        self.fetchAllUserFirstPostMedia(user: personBeignFollow)

       // tell the calling function to execute the completion handler again
       completion()
    }
}
然后要使用它,您可以执行以下操作:

getFollowers {
 // whatever you want to do after the query has run
}
func getFollowers(_ completion: (String) -> Void) {
    ref.child("users2").child((Auth.auth().currentUser?.uid)!).child("Following").observe(.childAdded) { (snap) in
        let personBeignFollow = snap.key
        print("Appened: ", personBeignFollow)        
       // tell the calling function to execute the completion handler again
       completion(personBeignFollow)
    }
}
这并不直接重要,但作为一种常见的设计实践,最好将从查询中检索到的新数据作为参数传递到完成处理程序中,而不是在类上分配属性

看起来是这样的:

getFollowers {
 // whatever you want to do after the query has run
}
func getFollowers(_ completion: (String) -> Void) {
    ref.child("users2").child((Auth.auth().currentUser?.uid)!).child("Following").observe(.childAdded) { (snap) in
        let personBeignFollow = snap.key
        print("Appened: ", personBeignFollow)        
       // tell the calling function to execute the completion handler again
       completion(personBeignFollow)
    }
}
然后,您的呼叫站点可能如下所示:

getFollowers { newUser in
    self.fetchAllUserFirstPostMedia(user: newUser)
}

如果查询位置上有8个项目,那么这不是运行8次吗?@NCT127是的
.childAdded
将对该位置的每个现有项触发一次,然后对调用函数后添加的每个项触发一次。但我想在获取所有数据并完成firebase调用后调用reload data。这就是我想要的achive@NCT127如果要避免对n个现有项调用该函数n次,可以使用
.observeSingleEvent(of:.value)
函数。这将查看该位置一次,然后返回到指定位置的所有内容。那么这是最好的方法吗?使用它,然后循环浏览我需要的项目?如果这是一个正确的解决方案,它会是什么样子?如果在查询位置有8个项目,它会不会运行8次?@NCT127是的
.childAdded
将对该位置的每个现有项触发一次,然后对调用函数后添加的每个项触发一次。但我想在获取所有数据并完成firebase调用后调用reload data。这就是我想要的achive@NCT127如果要避免对n个现有项调用该函数n次,可以使用
.observeSingleEvent(of:.value)
函数。这将查看该位置一次,然后返回到指定位置的所有内容。那么这是最好的方法吗?使用它,然后循环浏览我需要的项目?如果这是正确的解决方案,那会是什么样子?