IOS - Firebase Observe Single Event Calling Twice

I'm relatively new to Firebase and iOS, so I was hoping someone could give me some guidance on what I'm doing wrong. For some reason, the code block in my Observe is executed twice, although ObserveSingleEvent has to specifically stop monitoring after the initial data has been read.

Something that I want to note is that this only happens on my phone (iPhone 7 iOS10). I have a completely different problem, when I run it in the simulator, I get one call, as expected, however, the call is made while my view is still in the previous view. In fact, this function should be called after the main screen loads its view. However, in the simulator, when I authenticate my user on the login screen, it creates a pop-up window if I don’t have a username and the following view is not actually displayed on the main screen.

Here is the function causing the problems:

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) } }) } 

Here is my 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() } 

The result I get looks something like this:

 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) 

My Firebase data structure:

Screen shot

This is what happens when I print a snapshot

 observing event Snap (FJpA8PGCmwPRT9xjdrZENMIH8wJ3) { email = " test@gmail.com "; id = 10207417529265514; name = "Jason"; } 

I am happy to provide additional information if necessary, but I decided that it might be something in the function.

+6
source share

Source: https://habr.com/ru/post/1014499/


All Articles