0

This is my login view controller which controls the authentication and also has a function called "fetchVillaNumber()" which fetches data from FireStore. This function is the problem in my code... Even though this function works perfectly and fetches the correct villa number, I am not able to access the value fetched inside the closure in other parts of the code(outside the closure)... Can you please tell me how to access this fetched villa number value outside the closure? Thank you so much!

//  LoginViewController.swift
//  77 Safety Updater
//
//  Created by Tejas Ravishankar on 16/04/20.
//  Copyright © 2020 Tejas Ravishankar. All rights reserved.
//
//MARK:- Import Modules
import UIKit
import AVFoundation
import Firebase

var villa : String?

class LoginViewController: UIViewController
{
    var successfulAuthentication : Bool?
    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var errorLabel: UILabel!
    @IBOutlet weak var loadingWheel: UIActivityIndicatorView!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        emailTextField.delegate = self
        passwordTextField.delegate = self
        errorLabel.isHidden = true
        loadingWheel.isHidden = true
    }


    @IBAction func loginPressed(_ sender: UIButton)
    {
        //MARK:- Login Authentication
        loadingWheel.isHidden = false
        performSegue(withIdentifier: K.loginAnimationSegue ,sender: self)
        authenticate()
        sleep(7)
        fetchVillaNumber()
    }
}
//MARK:- UITextFieldDelegate Extension
extension LoginViewController : UITextFieldDelegate
{
    func textFieldShouldReturn(_ textField: UITextField) -> Bool
    {
        emailTextField.endEditing(true)
        passwordTextField.endEditing(true)
        return true
    }
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool
    {
        if emailTextField.text != "" || passwordTextField.text != ""
        {
            return true
        }
        else
        {
            return false
        }
    }
}

//MARK:- Authenticate And Fetch Villa Number
extension LoginViewController
{
    func authenticate() -> Bool
    {
        if let email = emailTextField.text, let password = passwordTextField.text
        {
            Auth.auth().signIn(withEmail: email, password: password)
            {
                [weak self] authResult,
                error in
                if let e = error
                {
                    self!.errorLabel.isHidden = false
                    self!.errorLabel.text = "\(e.localizedDescription)"
                    print(e.localizedDescription)
                    return
                }
                else
                {
                    let user = Auth.auth().currentUser?.uid
                    print("\n\n\n Hi I am in authenticate function : \(String(describing: user!))\n\n\n\n")
                    self!.errorLabel.isHidden = false
                    self!.errorLabel.textColor = .systemGreen
                    self!.errorLabel.text = "Successfully Authenticated!"
                    self!.loadingWheel.isHidden = false

                }
            }
        }
        successfulAuthentication = true
        return successfulAuthentication!
    }

func fetchVillaNumber()
{
    db.collection(K.FireStore.userCollection).getDocuments {(querySnapshot, error)  in
        if let e = error
        {
            print("There was a problem fetching data from FireStore \(e.localizedDescription)")
        }
        else
            {
                if let snapshotDocuments = querySnapshot?.documents
                {
                    for doc in snapshotDocuments
                    {
                        let data = doc.data()
                         let villaNumber = data[K.FireStore.villaNumber] as? String
                         let userID = data[K.FireStore.userID]
                            let user = Auth.auth().currentUser?.uid
                            if userID as? String == user
                            {
                                print(villaNumber)
                            }
                        }
                    }
                }
            }
        }
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
XtremeDevX
  • 1,482
  • 2
  • 13
  • 38

0 Answers0