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

dre*_*gin 6 amazon-web-services ios swift aws-appsync aws-appsync-ios

我正在将 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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

该代码在互联网连接下工作正常,如果没有互联网,我已经在顶部进行了检查,但是当互联网连接速度较慢或 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
Run Code Online (Sandbox Code Playgroud)

Kam*_*ran 6

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

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
    }()
}
Run Code Online (Sandbox Code Playgroud)

URLSessionConfiguration.customDelayed在初始化时传递此会话配置,AWSAppSyncClientConfiguration因为它接受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 {
Run Code Online (Sandbox Code Playgroud)

2)如果第一个不起作用,那么您还有另一个选项可以直接编辑/解锁 pod 文件。有一个类AWSAppSyncRetryHandler,您可以在其中更改重试请求的逻辑。如果您能够解决问题,那么您可以分叉原始存储库,克隆您的存储库,在存储库中进行更改,并在 pods 文件中指向此 pod 以使用您的存储库。应该这样做,因为直接更改 pod 文件是绝对错误的,直到您真的被卡住并想要找到一些解决方案。

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