如何设置 AWS Appsync 请求超时限制 || AWSAppSync 客户端不提供回调

How to set AWS Appsync request timeout limit || AWSAppSync Client not giving callback

我正在为我正在开发的当前应用程序使用 AWS Appsync,并面临一个严重的问题,即每当我在 Appsync 客户端中触发查询时,当互联网连接速度较慢时,请求永远不会以回调结束。我在互联网上查过关于这个主题的信息来源有限,而且 found this issue 仍然开放。

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

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 连接到我使用禁用互联网数据的手机创建的热点时该请求没有 return 任何回调,它应该像我们在请求超时时在其他 api 中获得的那样发出失败警报。 是否支持请求超时请求或我错过了什么?

Note : I recieved these logs in Terminal

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) 如果第一个不起作用,那么您可以选择直接 edit/unlock pod 文件。有一个 class AWSAppSyncRetryHandler ,您可以在其中更改重试请求的逻辑。如果您能够解决问题,那么您可以分叉原始存储库,克隆您的存储库,在您的存储库中进行更改,并在 pods 文件中指向此 pod 以使用您的存储库。应该这样做,因为直接更改 pod 文件是绝对错误的,直到你真的被卡住并想找到一些解决方案。

更新issue 已通过 AppSync SDK 2.7.0

修复