【发布时间】:2021-07-23 18:22:42
【问题描述】:
我正在尝试使用 firebase 和 swiftui 实现友谊模型。在我的一种观点中,在它的出现调用中,我调用了一个检查友谊状态的方法,如下所示:
编辑:我已将 isFriend 设为已发布的变量,该变量应按如下方式进行更新
class UserViewModel : ObservableObject{
....
//check friendship variabe
@Published var isFriend = 0
func checkFriendRequest(otherUserUID: String) -> Int{
//0 if not found in friend list or friend request
//1 means theyre friends
//2 means that user sent self/me a friend request
print("otherUserUID is \(otherUserUID)")
var pendingFriendRequests: [String: Bool] = [:]
self.ref.collection("Users").document(self.uid).getDocument(){
(document, err) in
if let err = err {
print("Error getting documents \(err)")
} else {
pendingFriendRequests = document!.data()!["friends"] as! [String : Bool]
print("pendingFriendRequests is \(pendingFriendRequests)")
for key in pendingFriendRequests.keys {
if key == otherUserUID{
print("key is \(key) and value is \(pendingFriendRequests[key])")
if pendingFriendRequests[key] == true {
self.isFriend = 1
}else if pendingFriendRequests[key] == false {
self.isFriend = 2
}
}
}
}
}
return self.isFriend
}
}
在我的视图代码中,我想检查这个函数的返回值并更新应该在视图上的按钮的文本/操作,它看起来如下所示:
struct OtherUser: View {
@StateObject var userData = UserViewModel()
var otherUserUID: String
var otherUserName: String
var otherUserUsername: String
var otherUserPic: String
@Environment(\.presentationMode) var present
init(_ otherUserUID: String, _ otherUserName: String, _ otherUserUsername: String, _ otherUserPic: String){
self.otherUserUID = otherUserUID
self.otherUserName = otherUserName
self.otherUserUsername = otherUserUsername
self.otherUserPic = otherUserPic
}
var body: some View {
ZStack(alignment: .top){
Color("Black")
.ignoresSafeArea()
VStack {
if (userData.checkFriendRequest(otherUserUID: otherUserUID)) == 1 {
//button for if returned 1
}else if (userData.checkFriendRequest(otherUserUID: otherUserUID)) == 2 {
//button for if returned 2
}else {
}
}
}
}
}
但是,当视图出现时,它会自动返回 0,而不会在异步 firebase 调用后更新值。我查找了其他解决方案并尝试了调度组,但它们似乎没有奏效。我知道它与 firebase 的异步特性有关,但希望得到一些帮助以了解如何改进这一点,以便变量 isFriend 得到更新,然后在视图出现之前返回。感谢您的帮助
【问题讨论】:
-
"...在视图出现之前得到更新然后返回"你问的是不可能的。如果网络速度慢或未连接怎么办?视图应该挂起/阻塞?您可以做的最好的事情是更新一个 \@State 或 \@ObservedObject (自动)触发视图以使用新结果更新自身。您没有显示您的视图代码...
-
感谢您的回复,我已经编辑了我的代码以包含已发布/观察到的对象,但它仍然无法正常工作并且只返回 0。我已经展示了我想要的视图代码根据返回 0、1 或 2 查看更改。谢谢
-
像 getDocument() 这样的 Firebase 调用是异步的。您不能在函数末尾返回值。你应该使用
if userData.isFriend,而不是if (userData.checkFriendRequest)。 -
那么我应该在哪里调用 checkFriendRequest() 函数来更新 isFriend 变量?
-
@HariKrishnaSenthilkumar 传统上类似于
onAppear
标签: ios swift firebase google-cloud-firestore swiftui