swift - 您如何访问 Alamofire 5 didCompleteTaskNotification 通知中的响应数据?
问题描述
我刚刚完成了从 Alamofire 4 到 5 的升级。除了记录响应以进行调试外,一切都很好。在 Alamofire 4 中,您可以访问响应数据。
NotificationCenter.default.addObserver(forName: NSNotification.Name.Task.DidComplete, object: nil, queue: OperationQueue.main) { notification in
if let responseData = notification.userInfo?[Notification.Key.ResponseData] as? Data {
if responseData.count > 0 {
let body = String(decoding: responseData, as: UTF8.self)
print("Response Body: \(body)")
}
}
}
在 Almaofire 5 中,您似乎无权访问响应数据。userInfo 中唯一的内容是通过 notification.request 访问的 Alamofire.Request。
NotificationCenter.default.addObserver(forName: Request.didCompleteTaskNotification, object: nil, queue: OperationQueue.main) { notification in
// no response data here
}
有谁知道如何访问响应数据?
解决方案
您可以Data
从Request
与通知相关联的信息中获取。
guard let request = notification.request as? DataRequest else { return }
// Do something with request.data
但是,我建议转换到我们EventMonitor
的日志记录协议,因为它可以让您访问更多事件。您可以在我们的文档中阅读更多内容。
推荐阅读
- inform7 - 在 Inform 7 中,进入容器的反义词是什么?
- mysql - MySQL问题找到当前薪酬最高的员工
- json - 如何加载json并提取到neo4j中的单独节点
- java - 我在 while 循环中有一个 for 循环,但没有到达
- python-3.x - 创建一个烧瓶应用程序以从 python3 中的 xlsx 文件返回字典列表
- javascript - Nodejs - 使用 Brightcove 生成的签名 URL 将文件上传到 AWS W3 返回 403 Forbidden
- azure-devops - 在多阶段 Azure DevOps Yamls 上迭代插入依赖和条件
- php - 使用逗号分隔行中的数据执行连接
- java - 如何使用 for 在字符串中进行迭代替换每个字符(A 与 T,T,A,G 与 C,C 与 G)?
- sql - 有没有类似于 Pandas tail() 的 SQLite 函数