Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using AWS Appsync for the current App I'm developing and facing a serious issue that is Whenever I fire queries in Appsync client, when there is slow internet connection the request never end with a callback. I checked over internet there is limited source of information on this topic and also found this issue that is still open.

This is the code I used to get the response

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)
        }
    }
}

The code works fine with internet connection and I have already checked at the top if there is no internet but when there is slow internet connection or the wifi is connected to a hotspot that I created with my mobile with internet data disabled the request doesn't return any callback it should give failed alert like we get in other apis when the request time out. Is there any support for request for request time out or did I miss something?

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
like image 423
tryKuldeepTanwar Avatar asked Oct 31 '18 12:10

tryKuldeepTanwar


1 Answers

Actually there could be two possible ways to fix the issue,

1) While configuring AWSAppSyncClientConfiguration, provide a custom URLSessionConfiguration and set the request timeout to your needs,

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
    }()
}

And pass this session configuration i.e URLSessionConfiguration.customDelayed when initializing AWSAppSyncClientConfiguration as it accepts the URLSessionConfiguration in the below constructor,

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) If the first doesn't work then you have another option to edit/unlock the pod files directly. There is a class AWSAppSyncRetryHandler where you can change the logic for retrying request. If you are able to fix the issue then you can fork the original repo, clone your repo, make changes in your repo and in pods file point this pod to use your repository. This should be done as changing the pod files directly is absolutely wrong until you are really stuck and want to find some solution.

Update: This issue has been fixed with AppSync SDK 2.7.0

like image 147
Kamran Avatar answered Nov 04 '22 01:11

Kamran