【问题标题】:How to set AWS Appsync request timeout limit || AWSAppSync Client not giving callback如何设置 AWS Appsync 请求超时限制 || AWSAppSync 客户端未提供回调
【发布时间】:2019-04-04 14:52:54
【问题描述】:

我正在为我正在开发的当前应用程序使用 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 连接到我用手机创建的热点时,互联网数据禁用了请求不返回任何回调它应该给出失败的警报,就像我们在请求超时时进入其他 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

【问题讨论】:

    标签: ios swift amazon-web-services aws-appsync aws-appsync-ios


    【解决方案1】:

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

    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,您可以在其中更改重试请求的逻辑。如果您能够解决问题,那么您可以分叉原始存储库,克隆您的存储库,在您的存储库中进行更改,并在 pods 文件中指向此 pod 以使用您的存储库。应该这样做,因为直接更改 pod 文件是绝对错误的,直到您真正陷入困境并想找到一些解决方案。

    更新:此issue 已修复为AppSync SDK 2.7.0

    【讨论】:

    • 感谢@Kamran 提供的所有信息,让我试试,我会尽快回复您。
    • 抱歉,我很难回复,@iOS_Developer 是的,因为没有其他可用的答案,所以这个答案为解决方案提供了可能的方向。我现在无法让第一个为第二个解决方案工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    相关资源
    最近更新 更多