Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 带alamofire的队列_Swift_Alamofire - Fatal编程技术网

Swift 带alamofire的队列

Swift 带alamofire的队列,swift,alamofire,Swift,Alamofire,当我使用Alamofire时,我对任务的执行有问题 我使用两次Alamofire,第一次收集数据(令牌),然后使用它发送Post请求 我的两个请求之间的问题是,数据的恢复在第二个请求之后完成 import Foundation import Alamofire import SwiftyJSON class Helper { func alomofireGet(URL: String) -> JSON { let queue = DispatchQueue(lab

当我使用Alamofire时,我对任务的执行有问题 我使用两次Alamofire,第一次收集数据(令牌),然后使用它发送Post请求

我的两个请求之间的问题是,数据的恢复在第二个请求之后完成

import Foundation
import Alamofire
import SwiftyJSON

class Helper {
    func alomofireGet(URL: String) -> JSON {
        let queue = DispatchQueue(label: "com.test.com", qos: .background, attributes: .concurrent)
        var contenuJSON = JSON()
        Alamofire.request(URL, method: .get).responseJSON(queue: queue) { (reponse) in
            if reponse.result.isSuccess {
                contenuJSON = JSON(reponse.result.value!)
                print(contenuJSON)
            }
            else {
                contenuJSON = JSON(reponse.result.error!)
            }
        }
        return contenuJSON
    }
    func alomofirePost(URL: String, Paramaters: Dictionary<String, Any>) -> JSON {
        var contenuJSON = JSON()
        Alamofire.request(URL, method: .post, parameters: Paramaters, encoding: JSONEncoding.default).responseJSON { (reponse) in
            if reponse.result.isSuccess {
                contenuJSON = JSON(reponse.result.value!)
            }
            else {
                contenuJSON = JSON(reponse.result.error!)
            }
        }
        return contenuJSON
    }
}

}对Alamofire的API调用是异步过程,因此您的
AlamoFileGet
alamofirePost
返回刚刚初始化的json对象-
json()
,该对象没有任何数据

解决方案:

您应该使用
@escaping closure
,它将保持控件,直到您从第一个API调用获得结果为止

func alomofireGet(URL: String, onCompletion:((JSON) -> Void)) {
    let queue = DispatchQueue(label: "com.test.com", qos: .background, attributes: .concurrent)
    var contentJSON = JSON()
    Alamofire.request(URL, method: .get).responseJSON(queue: queue) { (reponse) in
        // Load contentJSON with data
        if reponse.result.isSuccess {
            contenuJSON = JSON(reponse.result.value!)
        } else {
            contenuJSON = JSON(reponse.result.error!)
        }
        // Send contentJSON via `onCompletion` block
        onCompletion(contenuJSON)
    }
}

func alomofirePost(URL: String, Paramaters: Dictionary<String, Any>, onCompletion: @escaping ((_ response: JSON) -> Void)) {
    var contenuJSON = JSON()
    Alamofire.request(URL, method: .post, parameters: Paramaters, encoding: JSONEncoding.default).responseJSON { (reponse) in
        // Load contentJSON with data
        if reponse.result.isSuccess {
            contenuJSON = JSON(reponse.result.value!)
        } else {
            contenuJSON = JSON(reponse.result.error!)
        }
        // Send contentJSON via `onCompletion` block
        onCompletion(contenuJSON)
    }
}

您缺少异步的概念
returncontenujson
将在
contenuJSON=JSON(…)
之前调用(您可以添加打印,您将看到)。寻找“Swift+Closure+Async”,您需要使用
completion
block。您可以从此处应用我尝试了此操作,但出现了新的错误消息=“UITextField.text必须仅从主线程使用'I'v'尝试使用DispatchQueue.main.async,但不起作用。可能是一个坏习惯尝试在另一个变量中使用它,或者可以使用文本字段的弱引用。更新了我的答案。@AnkitJayaswal我能问一个与
Alamofire synchronous
相关的问题吗?
func alomofireGet(URL: String, onCompletion:((JSON) -> Void)) {
    let queue = DispatchQueue(label: "com.test.com", qos: .background, attributes: .concurrent)
    var contentJSON = JSON()
    Alamofire.request(URL, method: .get).responseJSON(queue: queue) { (reponse) in
        // Load contentJSON with data
        if reponse.result.isSuccess {
            contenuJSON = JSON(reponse.result.value!)
        } else {
            contenuJSON = JSON(reponse.result.error!)
        }
        // Send contentJSON via `onCompletion` block
        onCompletion(contenuJSON)
    }
}

func alomofirePost(URL: String, Paramaters: Dictionary<String, Any>, onCompletion: @escaping ((_ response: JSON) -> Void)) {
    var contenuJSON = JSON()
    Alamofire.request(URL, method: .post, parameters: Paramaters, encoding: JSONEncoding.default).responseJSON { (reponse) in
        // Load contentJSON with data
        if reponse.result.isSuccess {
            contenuJSON = JSON(reponse.result.value!)
        } else {
            contenuJSON = JSON(reponse.result.error!)
        }
        // Send contentJSON via `onCompletion` block
        onCompletion(contenuJSON)
    }
}
    let usernameStr = self.emailText.text!
    let passwordStr = self.passwordText.text! 

    Helper().alomofireGet(URL: "http://192.168.1.7/app_dev.php/login/app") { contenuJSON in
        print(contenuJSON)

        DispatchQueue.main.async {
            let token = contenuJSON["csrfToken"].stringValue
            print(token)

            let Paramaters = ["_csrf_token": token, "_password": passwordStr, "_redirect_url": "", "t_path": "", "_username": usernameStr]
            Helper().alomofirePost(URL: "http://192.168.1.7/app_dev.php/login_check", Paramaters: Paramaters) { contenuJSON in
                print(token)
            }
        }

    }