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(
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:

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.