作者:amroc_394 | 来源:互联网 | 2023-02-13 17:41
我对Firebase和iOS开发相对较新,所以我希望有人能给我一些关于我做错的指导.出于某种原因,我的Observe中的代码块正在运行两次,即使ObserveSingleEvent特别应该在读取初始数据后终止观察.
我想要注意的是,这只发生在我的手机上(iPhone 7 iOS10).当我在模拟器中运行它时,我遇到了一个完全不同的问题,我得到了一个应该调用的调用,但是,当我的视图仍在上一个视图中时调用.本质上,应该在主屏幕加载其视图后调用此函数.但是,在模拟器中,当我在登录屏幕中对我的用户进行身份验证时,如果我没有用户名,则会创建弹出窗口,并且实际上不会将下一个视图显示到主屏幕.
这是引起问题的函数:
func initialSetUp() {
print("initially set up")
let userRef = FIRDatabase.database().reference(withPath: "/users/").child("\(uid!)")
userRef.observeSingleEvent(of: .value, with: { (snapshot) in
print("observing event")
print("\(snapshot)")
if snapshot.hasChild("username"){
print("user has username")
} else {
print("user does not have username")
//do stuff
let nameRef = FIRDatabase.database().reference(withPath: "/usernames/")
let alert = SCLAlertView()
let appearance = SCLAlertView.SCLAppearance(
kTitleFont: UIFont(name: "Futura-Medium", size: 20)!,
kTextFont: UIFont(name: "Futura-Medium", size: 14)!,
kButtonFont: UIFont(name: "Futura-Bold", size: 14)!,
showCloseButton: false,
shouldAutoDismiss: false
)
alert.appearance = appearance
let newName = alert.addTextField("Enter your name")
alert.addButton("Check name", backgroundColor: UIColor.black, textColor: UIColor.white, showDurationStatus: false) {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to:nil, from:nil, for:nil)
if let name = newName.text {
if name != "" {
//check if username is unique
nameRef.child(name.lowercased()).observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in
print("\(snapshot.value)")
if !snapshot.exists() {
let alertView = SCLAlertView()
alertView.showSuccess("Woohoo!", subTitle: "This username is valid!")
print("valid username")
} else {
let alertView = SCLAlertView()
alertView.showError("Sorry :(", subTitle: "This username has already been taken.")
}
})
} else {
let alertView = SCLAlertView()
alertView.showError("Sorry :(", subTitle: "Your name can't be blank!")
}
}
}
alert.addButton("Done", backgroundColor: UIColor.black, textColor: UIColor.white, showDurationStatus: false) {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to:nil, from:nil, for:nil)
if let name = newName.text {
if name != "" {
//check if username is unique
nameRef.child(name.lowercased()).observeSingleEvent(of: FIRDataEventType.value, with: { (snapshot) in
print("\(snapshot.value)")
if !snapshot.exists() {
alert.hideView()
let alertView = SCLAlertView()
alertView.showSuccess("Success!", subTitle: "You're all set to go!")
userRef.child("username").setValue(name)
nameRef.updateChildValues([name.lowercased(): self.uid])
} else {
let alertView = SCLAlertView()
alertView.showError("Sorry :(", subTitle: "This username has already been taken.")
}
})
} else {
let alertView = SCLAlertView()
alertView.showError("Sorry :(", subTitle: "Your name can't be blank!")
}
}
}
alert.showCustom("Username", subTitle: "Please pick a user name!", color: UIColor.black, icon: SCLAlertViewStyleKit.imageOfEdit)
}
})
}
这是我的viewDidLoad
override func viewDidLoad() {
self.view.addSubview(self.mapViewController.view)
self.view.addSubview(self.tableController.view)
print("View did load")
uid = (FIRAuth.auth()?.currentUser?.uid)!
locatiOnsRef= FIRDatabase.database().reference(withPath: "/users/").child("\(uid)").child("locations")
initialSetUp()
}
我收到的输出看起来像这样
View did load
initially set up
observing event
user does not have username(or user does have username if I use a different account)
observing event
user does not have username(or user does have username if I use a different account)
我的Firebase数据结构:
以下是打印快照时发生的情况
observing event
Snap (FJpA8PGCmwPRT9xjdrZENMIH8wJ3) {
email = "test@gmail.com";
id = 10207417529265514;
name = "Jason";
}
如果需要,我很乐意提供更多信息,但我认为它可能是函数中的内容.