Alamofire同步请求

发布于 2025-01-11 16:36:46 字数 1479 浏览 0 评论 0原文

我正在尝试使用 Alamofire 5 对后端进行登录调用。问题是当我进行调用时,我需要一个值返回到控制器以验证凭据。

所以,问题是 Alamofire 只进行异步调用,所以我需要使其同步。我看到了一个使用信号量的解决方案,但我不知道如何实现它。

这是我找到的解决方案:

func syncRequest(_ url: String, method: Method) -> (Data?, Error?) {
    var data: Data?
    var error: Error?
    let url = URL(string: url)!
    var request = URLRequest(url: url)
    request.httpMethod = method.rawValue
    let semaphore = DispatchSemaphore(value: 0)
    let dataTask = URLSession.shared.dataTask(with: request) {
        data = $0
        error = $2
        semaphore.signal()
    }
    dataTask.resume()
    _ = semaphore.wait(timeout: .distantFuture)
    return (data, error)
}

而且,这是我的请求代码:

AF.request(request)
        .uploadProgress { progress in
            
        }
        .response(responseSerializer: serializer) { response in
            if response.error == nil {
                if response.data != nil {
                    do {
                        try decoder.decode(LogInSuccessful.self, from: response.data!)
                        
                    } catch {
                        do {
                            try decoder.decode(LogInError.self, from: response.data!)
                            
                        } catch {
                            
                        }
                    }
                }
                statusCode = response.response!.statusCode
            }
        }

I'm trying to make a Log In Call to the backend using Alamofire 5. The problem is when I make the call I need a value to return to the Controller to validate the credentials.

So, the problem is Alamofire only make asynchronous calls so I need to make it synchronous. I saw a solution using semaphore but I don't know how implement it.

This is the solution that I found:

func syncRequest(_ url: String, method: Method) -> (Data?, Error?) {
    var data: Data?
    var error: Error?
    let url = URL(string: url)!
    var request = URLRequest(url: url)
    request.httpMethod = method.rawValue
    let semaphore = DispatchSemaphore(value: 0)
    let dataTask = URLSession.shared.dataTask(with: request) {
        data = $0
        error = $2
        semaphore.signal()
    }
    dataTask.resume()
    _ = semaphore.wait(timeout: .distantFuture)
    return (data, error)
}

And, this is my request code:

AF.request(request)
        .uploadProgress { progress in
            
        }
        .response(responseSerializer: serializer) { response in
            if response.error == nil {
                if response.data != nil {
                    do {
                        try decoder.decode(LogInSuccessful.self, from: response.data!)
                        
                    } catch {
                        do {
                            try decoder.decode(LogInError.self, from: response.data!)
                            
                        } catch {
                            
                        }
                    }
                }
                statusCode = response.response!.statusCode
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文