gpt4 book ai didi

ios - 处理 iOS 应用程序的长网络请求

转载 作者:行者123 更新时间:2023-11-28 19:28:30 24 4
gpt4 key购买 nike

我想让我的应用程序在从服务器获取数据时出现问题时响应更快。

现在我正在使用 Reachability 库来检查对 Internet 的访问,但我认为这还不够。有时服务器会处理很长时间的请求,甚至宕机不响应。

在网络请求花费太多时间的情况下,我想通知用户出了点问题,而不是显示无休止的事件指示器。

从哪里开始?最佳做法是什么?

最佳答案

Set a custom timeout interval for the URLRequest. It should be ideally depending on the amount of data you expect the server to send and the internal queries the server does to fetch the data and finally dump the same to the API you are using.

尝试使用 postman 了解服务器响应时间和您收到的数据。根据该估计为您的请求合理的超时间隔。

let request = NSMutableURLRequest()
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.timeoutInterval = 20 // this is what you need to set.

然后您可以通过一些消息向用户提供消息或响应。就连接性而言,您可以查看 YouTube 应用,尝试将设备置于飞行模式,您会看到显示连接状态的 strip ,而不会影响用户体验。

关于ios - 处理 iOS 应用程序的长网络请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49003869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com