如何设置AWS Appsync请求超时限制|| AWSAppSync客户端未进行回调



我正在为我正在开发的当前应用程序使用AWS Appsync,并面临一个严重的问题,即每当我在Appsync客户端中启动查询时,当互联网连接缓慢时,请求永远不会以回调结束。我在网上查了一下,关于这个话题的信息来源有限,也发现这个问题仍然悬而未决。

这是我用来获取响应的代码

func getAllApi(completion:@escaping DataCallback){
guard isInternetAvailabele() else {
completion(nil)
return
}
// AppSyncManager.Client() is AWSAppSyncClient Object
AppSyncManager.Client().fetch(query: GetlAllPostQuery(input: allInputs), cachePolicy:.fetchIgnoringCacheData) {
(result, error) in
var haveError:Bool = error != nil
if let _ = result?.data?.getAllPostings?.responseCode {haveError = false} else {haveError = true}
if haveError  {
print(error?.localizedDescription ?? "")
completion(nil)
return
}
if result != nil{
completion(result)
}else{
completion(nil)
}
}
}

该代码在互联网连接中运行良好,我已经在顶部检查了是否没有互联网,但当互联网连接缓慢或wifi连接到我用手机创建的热点(禁用了互联网数据(时,请求不会返回任何回调,它应该会在请求超时时发出失败警报,就像我们在其他api中看到的那样。是否支持请求超时或我错过了什么?

注意:我在终端中收到了这些日志

Task <06E9BBF4-5731-471B-9B7D-19E5E504E57F>.<45> HTTP load failed (error code: -1001 [1:60])
Task <D91CA952-DBB5-4DBD-9A90-98E2069DBE2D>.<46> HTTP load failed (error code: -1001 [1:60])
Task <06E9BBF4-5731-471B-9B7D-19E5E504E57F>.<45> finished with error - code: -1001
Task <D91CA952-DBB5-4DBD-9A90-98E2069DBE2D>.<46> finished with error - code: -1001

实际上有两种可能的方法来解决这个问题,

1(配置AWSAppSyncClientConfiguration时,提供自定义URLSessionConfiguration并根据您的需要设置请求timeout

extension URLSessionConfiguration {
/// A `URLSessionConfiguration` to have a request timeout of 1 minutes.
static let customDelayed: URLSessionConfiguration = {
let secondsInOneMinute = 60
let numberOfMinutesForTimeout = 1
let timoutInterval = TimeInterval(numberOfMinutesForTimeout * secondsInOneMinute)
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = timoutInterval
configuration.timeoutIntervalForResource = timoutInterval
return configuration
}()
}

并在初始化AWSAppSyncClientConfiguration时传递此会话配置,即URLSessionConfiguration.customDelayed,因为它在下面的构造函数中接受URLSessionConfiguration

public convenience init(url: URL,
serviceRegion: AWSRegionType,
credentialsProvider: AWSCredentialsProvider,
urlSessionConfiguration: URLSessionConfiguration = URLSessionConfiguration.default,
databaseURL: URL? = nil,
connectionStateChangeHandler: ConnectionStateChangeHandler? = nil,
s3ObjectManager: AWSS3ObjectManager? = nil,
presignedURLClient: AWSS3ObjectPresignedURLGenerator? = nil) throws {

2(如果第一个不起作用,那么您可以选择直接编辑/解锁pod文件。有一个类AWSAppSyncRetryHandler,您可以在其中更改重试请求的逻辑。如果你能够解决这个问题,那么你可以分叉原始回购,克隆你的回购,在你的回购中进行更改,并在pod文件中指向这个pod来使用你的存储库。这应该是因为直接更改pod文件是绝对错误的,直到你真的陷入困境并想找到一些解决方案。

更新:此问题已通过AppSync SDK 2.7.0修复

最新更新