gpt4 book ai didi

swift - 在 for 循环完成快速处理 Firebase 快照之前,处理程序被提前调用,为什么?

转载 作者:行者123 更新时间:2023-11-30 11:03:51 24 4
gpt4 key购买 nike

我需要一些帮助,我有以下代码,它是从 Firebase 查询并返回用户数据的快照的函数的一部分。我试图循环遍历 snapshot 将其传递给帮助器 filterDiscoveredUserByPreferences 方法,该方法有助于过滤输入/输出,然后它应该将对象添加到数组中,以便它可以通过处理程序传回。

问题是,在 for 循环完成并处理从 Firebase 返回的快照之前,我的 handler 被过早调用。我尝试过使用 distpatchQueue 但没有成功。

self.getUsersAtVenueHandle = self.REF_USERS.observe(.value, with: { (snapshot) in

guard let usersSnapshot = snapshot.children.allObjects as? [DataSnapshot] else { return }


for user in usersSnapshot{
// Code here to get all values from ‘user’ snapshot the create User object

let user = User(uid: uid, dictionary: dictionary)

if user.discoverable == true && user.uid != Auth.auth().currentUser?.uid {

self.filterDiscoveredUserByPreferences(discoveredUser: user, handler: { (success) in

if success {
users.append(user)
}

})//filterDiscoveredUserByPreferences

}//end if

}//end for

//handler gets called before for loops has finished processing the entire snapshot

handler (users, true)
users.removeAll()


}, withCancel: { (error) in
print(error)

})//end Firebase call

功能:filterDiscoveredUserByPreferences

func filterDiscoveredUserByPreferences(discoveredUser: User, handler: @escaping (Bool) -> ()){

let discoveredGender = discoveredUser.gender
let discoveredPrefs: [String : Any] = discoveredUser.discoveryPrefs
let discoveredPreferMen = discoveredPrefs["men"] as? Bool
let discoveredPreferWomen = discoveredPrefs["women"] as? Bool

var discoveredPreferredGender: String?

switch (discoveredPreferMen, discoveredPreferWomen) {

case (true, true):
discoveredPreferredGender = "both"

case (true, false):
discoveredPreferredGender = "female"

case (false, true):
discoveredPreferredGender = "male"

case (false, false):
discoveredPreferredGender = "not discoverable"

default:
break
}//end switch


getUserInfo(forUserId: (Auth.auth().currentUser?.uid)!) { (user) in

let myGender = user.gender
let myDiscoveryPrefs: [String : Any] = user.discoveryPrefs
let myPreferMen = myDiscoveryPrefs["men"] as? Bool
let myPreferWomen = myDiscoveryPrefs["women"] as? Bool


switch (myPreferWomen, myPreferMen) {

case (true, true): //both

switch myGender {

case "male":

switch(discoveredPreferredGender){

case "male":
handler(true)

case "female":
handler(false)

case "both":
handler(true)

case "not discoverable":
handler(false)

default:
break

}//end switch

case "female":

switch(discoveredPreferredGender){

case "male":
handler(false)

case "female":
handler(true)

case "both":
handler(true)

case "not discoverable":
handler(false)

default:
break

}//end switch

default:
break

}//end switch



case (true, false)://female only

if discoveredGender == "male"{
handler(false)
} else {

switch(discoveredPreferredGender){

case "male":
handler(true)

case "female":
handler(false)

case "both":
handler(true)

case "not discoverable":
handler(false)

default:
break

}//end switch

}//end else

case (false, true): //male only

if discoveredGender == "female"{
handler(false)
} else {

switch(discoveredPreferredGender){

case "male":
handler(true)

case "female":
handler(false)

case "both":
handler(true)

case "not discoverable":
handler(false)

default:
break

}//end switch

}//end else

case (false, false): //none

handler(false)

default:
break
}


}//end getLoggedinUserDetails


}//end fun

函数 getUserInfo

func getUserInfo(forUserId: String, currentUserHandler: @escaping (_ currentUser: User) -> ()){

REF_USERS.child(uid).observe(.value, with: { (snapshot) in

guard let dict = snapshot.value as? [String: Any] else { return }

let user = User(uid: uid, dictionary: dict)

currentUserHandler(user)

}) { (error) in
print(error)
}

}//end func

最佳答案

这是异步回调,它会被多次调用,具体取决于更新。如果您使用调试断点来检查这一点,您可能会感到困惑。在 observe(.value, with: { (snapshot) } 的开头和结尾写入控制台日志闭包,你就会理解流程。在完成 for 循环之前不可能调用处理程序。可能是您的第一个回调有大量记录,第二个回调有零 DataSnapshot

关于swift - 在 for 循环完成快速处理 Firebase 快照之前,处理程序被提前调用,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52980065/

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