我在UserKM
类中有一个方法声明,例如以下代码:
static func getUserData (userID: String, completion: @escaping (UserKM?)->Void) {
FirestoreDocumentReference.users(uidUser: userID).reference().getDocument { (snapshot, error) in
if let error = error {
print("Failed to get the user data: \(error.localizedDescription)")
completion(nil)
} else {
print("Successfully get the user data from Firestore")
guard let docSnaphot = snapshot, docSnaphot.exists else {
completion(nil)
return
}
guard let myUserData = docSnaphot.data() else {return}
let user = UserKM(dictionary: myUserData)
completion(user)
}
}
}
,该getUserData
方法将在视图控制器的下面函数UserKM.getUserData
中使用
func getUserKMData() {
userData = Auth.auth().currentUser
guard let userID = userData?.uid else {return}
SVProgressHUD.show(withStatus: "Harap Tunggu")
UserKM.getUserData(userID: userID) { (userKumpMuslim) in
guard let userKumus = userKumpMuslim else {return}
self.userKumpulMuslim = userKumus
self.setDomicileButton()
userKumus.saveUserKMBasicDataToKeychain()
self.hasBeenFetchedMoreThan24HoursAgo = self.checkIfItHasBeenFetchedForTheLast24HoursOrNot()
if self.hasBeenFetchedMoreThan24HoursAgo {
self.getRecommendedEvents()
} else {
self.readRecommendedEventsInUserDefault()
}
}
}
但是我不明白为什么无论何时更新用户数据,总是会自动触发该UserKM.getUserData
方法。
我没有使用侦听器,但似乎这种行为类似于使用.addSnapshotListener
,但是我在这里使用.getDocument
如何使其不自动触发?