How to modify the value of a global variable from Alamofire


For example, I need to change the value of the variable existUser which by default is initialized as false to true but when the variable is finished it being in false and only the value true is kept within the response of Alamofire

import Foundation
import Alamofire

class UserLoginModel {

let URL_PRODUCCION = "url-de-mi-servidor"

var username: String
var userpassword: String
var usercompany: String

var existUser: Bool?

init(username: String, userpassword: String, usercompany: String){
    self.username = username
    self.userpassword = userpassword
    self.usercompany = usercompany

func test(){

    let url = "\(URL_PRODUCCION)"

    let parametros: Parameters = [

    Alamofire.request(url, method: .post, parameters: parametros) .responseJSON {
        response in

        if let JSON = response.result.value{
            self.existUser = true
            self.existUser = false

func setExistUser(existUser: Bool){
    self.existUser = existUser

func getExistUser(){
    return self.existUser


From the controller I create an object of type UserLoginModel, then I call the method test () , then I call the method getExistUser () , but it gives me an error and says < strong> existUser is equal to nil .

Anyway, I could not assign a value from within Alamofire even with the setUserExist () method, I hope you have a clearer day now and you can help me help, thank you in advance.


class ViewController: UIViewController {

var user: String?
var password: String?
var company: String?

var usuario: UserLoginModel?

override func viewDidLoad() {
    // Do any additional setup after loading the view, typically from a nib.

override func didReceiveMemoryWarning() {
    // Dispose of any resources that can be recreated.

@IBAction func btnIngresarClick(_ sender: Any) {

    self.user = textFieldUserName.text!
    self.password = textFieldUserPassword.text! = textFieldUserCompany.text!

    self.usuario = UserLoginModel(username: self.user!, userpassword: self.password!, usercompany:!)

        //Haga esto
        //Haga esto otro

        //Siempre está entrando por aquí, si 'existUser' la inicializo en 'false' siempre me devuelve 'false',
        //pero sí la dejo como un 'optional' siempre me retorna 'nil'

asked by yomar_crm 14.07.2017 в 17:43

1 answer


The problem is that the call you make asynchronously, that is, when you call if(self.usuario?.getExistUser()){ the call to the server that you use with Alamofire may still be running, without getting an answer and therefore, the value of existUser is false yet.

What I recommend is that the test method gives you as an input parameter a block that runs within the success of the Alamofire call.

Something like:

func test(completionBlock: (Bool) -> ())

Then I should call him:

self.usuario?.test(completionBlock: (success) { //Agregas tu lógica donde ya tienes la respuesta de Alamofire })

Finally, within the success of Alamofire you call the block with the same variable of the model or another that you consider pertinent:

if let JSON = response.result.value { self.existUser = true completionBlock(self.existUser) } else{ self.existUser = false completionBlock(self.existUser) }

I hope it works.

answered by 17.07.2017 в 22:06