Search code examples
swiftfunctionswift3handlercompletion

Function will not call Swift 3


@IBAction func signup(_ sender: Any) {

    print("began signup process")

    guard let fullname = fullnameField.text, fullname != "" else {
        print("FULLNAME field is empty")
        return
    }

    guard let username = usernameField.text, username != "" else {
        print("USERNAME field is empty")
        return
    }

    guard let email = emailField.text, email != "" else {
        print("EMAIL field is empty")
        return
    }

    guard let password = passwordField.text, password != "" else {
        print("PASSWORD field is empty")
        return
    }

    print("all fields good")

    mainActivityIndicator.startAnimating()

    self.checkUsernameAvailability(username: username, completion: {
        result in
        print("starting check")
        ...
    })


    print("finished the function")
}

The issue is basically that nothing inside of the checkUsernameAvailability function will call. The console looks like this:

began signup process
all fields good
finished the function

it does not print 'starting check' or run any code at all inside of the function. This is probably a rookie error and I am sorry if it is a stupid question. Future thanks.

P.S I checked the entire console and there is no error relating to this.

EDIT: Here is the code inside the function

func checkUsernameAvailability(username: String, completion: @escaping (Bool) -> Void) {
    _ = Database.database().reference().child("usernames").observe(.childAdded, with: {
        snapshot in

        print("checking username availability")
        print(snapshot)

        completion(true)

        let dict = snapshot.value as? [String: AnyObject]
        for handled in (dict?.values)! {

            print("stumbled")
            print(handled)
            if username.lowercased() == handled.lowercased {
                completion(false)
            }
        }
    })
}

And...

self.checkUsernameAvailability(username: username, completion: {
        result in

        print("starting check")

        if result == false {
            print("username is taken")
            self.mainActivityIndicator.stopAnimating()
            return
        } else {

            Auth.auth().createUser(withEmail: email, password: password, completion: {
                (user, error) in

                if error != nil {
                    print(error ?? "ERROR OCCURED")
                    self.mainActivityIndicator.stopAnimating()
                    return
                }

                let ref = Database.database().reference()
                ref.child("~/users/\(user?.uid ?? "0")/username").setValue(username.lowercased())
                ref.child("~/users/\(user?.uid ?? "0")/fullname").setValue(fullname)
                ref.child("usernames/\(user?.uid ?? "0")").setValue(username.lowercased())

                self.mainActivityIndicator.stopAnimating()
            })

        }
    })

Solution

  • The Database.database().reference().child("usernames").observe call simply never calls the with block. I would assume that block is called when the event .childAdded is observed, but I see no code that would add a child. To me this looks like you are setting up an asynchronous observer, so this code will not run when you make the call, it will run when the monitored event takes place.