0

This might have been asked here but I couldn't find a solution.

I'm making a search function and if a user puts an email matched in the firebase database, I want to change the value of the result checking variable(checkResult) below.

I know how it doesn't work, but I can't find a solution.

    guard let emailSearch = searchBar.text else { return }

    var checkResult = false

    let ref = Database.database().reference()
    ref.child("users").observe(.childAdded, with: { (snapshot) in

        guard let dictionary = snapshot.value as? [String: Any] else { return }
        let user = User(dictionary: dictionary)

        if user.email == emailSearch {
            checkResult = true
        }

    }) { (err) in
        print("Error: ", err)
    }

    print(checkResult)
Frank van Puffelen
  • 499,950
  • 69
  • 739
  • 734
UK4
  • 429
  • 5
  • 18
  • I can handle tableView like that in the link but I'm trying to figure out how to change the variable value and use it outside of closure. – UK4 Apr 24 '18 at 05:38
  • The point of the link wasn't that - the point was to demonstrate you can only use the variable once it's populated within the closure. In your code, the print statement will execute *before* the code in the closure. Move the print statement right after the *if user.email* closure and will print what you expect. If you want something more we would need to understand in what context you want to use the checkResult variable. – Jay Apr 27 '18 at 17:28

0 Answers0