Swift Alamofire 异步问题
Posted
技术标签:
【中文标题】Swift Alamofire 异步问题【英文标题】:Swift Alamofire Async Issue 【发布时间】:2021-06-10 04:41:19 【问题描述】:这里是 swift 的新手。
我正在尝试在另一个 AF.request 调用中进行 AF.request 调用,一切正常。
问题是fetchAllUsers()
在所有内容加载完毕后被调用。因此,我必须刷新页面才能让fetchAllUsers()
执行,而不是立即获取所有用户。
我认为使用闭包可以避免这个问题,但它仍在发生。
我做错了吗?
func fetchAllUsers(completionHandler: @escaping ([User]) -> Void)
let allUsersUrl = baseUrl + "users/"
let headers: HTTPHeaders = [
.authorization(bearerToken: NetworkManager.authToken)
]
if let url = URL(string: allUsersUrl)
AF.request(url, headers: headers).responseDecodable(of: [User].self) response in
if let users = response.value
completionHandler(users)
else
print("Users is empty")
else
print("URL error")
func login(param: [String:String], completionHandler: @escaping (Int) -> Void)
let loginUrl = baseUrl + "auth/login"
// Sets the Logged In User
AF.request(loginUrl, method: .post, parameters: param, encoding: JSONEncoding.default, headers: nil).responseString response in
if let data = response.data
let authTokenString = String(decoding: data, as:UTF8.self).components(separatedBy: " ")
// Sets the authentication token
NetworkManager.authToken = authTokenString[1]
self.fetchAllUsers users in
for user in users
if param["username"] == user.username
HomeViewModel.loggedInUser = user
completionHandler(response.response!.statusCode)
【问题讨论】:
【参考方案1】:将主函数的完成处理程序和状态码传递给 fetchAllUsers 并在它自己的完成处理程序之后调用它
completionHandler(response.response!.statusCode) 在 self.fetchAllUsers 关闭 之前执行,因为它正在等待 api 响应完成。 completionHandler(response.response!.statusCode) 正在销毁 self.fetchAllUsers 闭包 在执行之前,所以我在 self.fetchAllUsers 中调用了 completionHandler(response.response!.statusCode)关闭后
completionHandler(users)
mainCompletion(statusCode)
完整的代码如下
func fetchAllUsers(statusCode:Int, mainCompletion: @escaping (Int) -> Void, completionHandler: @escaping ([User]) -> Void)
let allUsersUrl = baseUrl + "users/"
let headers: HTTPHeaders = [
.authorization(bearerToken: NetworkManager.authToken)
]
if let url = URL(string: allUsersUrl)
AF.request(url, headers: headers).responseDecodable(of: [User].self) response in
if let users = response.value
completionHandler(users)
mainCompletion(statusCode)
else
print("Users is empty")
else
print("URL error")
func login(param: [String:String], completionHandler: @escaping (Int) -> Void)
let loginUrl = baseUrl + "auth/login"
// Sets the Logged In User
AF.request(loginUrl, method: .post, parameters: param, encoding: JSONEncoding.default, headers: nil).responseString response in
if let data = response.data
let authTokenString = String(decoding: data, as:UTF8.self).components(separatedBy: " ")
// Sets the authentication token
NetworkManager.authToken = authTokenString[1]
self.fetchAllUsers(statusCode: response.response!.statusCode, mainCompletion: completionHandler) users in
for user in users
if param["username"] == user.username
HomeViewModel.loggedInUser = user
【讨论】:
您好,所以代码按我想要的方式工作。谢谢你。您能否简要解释一下为什么这是有效的以及为什么我之前的无效? 当然,更新的答案和解释请让我知道如果还有问题@sameTbh以上是关于Swift Alamofire 异步问题的主要内容,如果未能解决你的问题,请参考以下文章
iOS/IBM Cloud/Swift:使用 AlamoFire 发布到 Watson API