I am fetching a list of items using Alamofire and Promisekit. Some items take longer to get than others. I have set the timeout in alamoFireManager
to 180 seconds, however 60 seconds after the first getRemoteItem
is called, all the remaining calls for items which have not been fetched in the list will fall into the print("IN FAIL 2")
. The error reads `
responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around line 1, column 0." UserInfo={NSDebugDescription=Invalid value around line 1, column 0., NSJSONSerializationErrorIndex=0}))
The initial getItems
call will call for each item in parallel which is where I am guessing the problem may be but I cant figure out a workaround for this or why the 180 second timeout is not respected.
func getItems(ids:[Int]) -> [Promise<Bool>] {
let totalCount = ids.count
return ids.map { index -> Promise<Bool> in
return firstly {
getRemoteItem(id: index, totalCount: totalCount)
}
}
}
func getRemoteItem(id:Int, totalCount:Int) -> Promise<Bool> {
let headers = ["Authorization": "JWT \(userToken)"]
return Promise { seal in
let url = baseURL + "/api/items"
let itemParams: Parameters = ["item_id": id]
alamoFireManager!.request(URL(string: url)!, method: .post, parameters: itemParams, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
switch response.result {
case .success:
if let jsonObject = response.result.value {
firstly {
self.saveItemToLocalStorage(jsonObject: jsonObject as! [String: Any], responseData: response.data!, totalCount: totalCount)
}.done { status in
seal.fulfill(status)
}.catch { error in
seal.reject(error)
}
} else if let error = response.error {
print("IN FAIL 1")
seal.reject(error)
}
case .failure(let error):
print("IN FAIL 2")
seal.reject(error)
}
}
}
}
private lazy var alamoFireManager: SessionManager? = {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 180
configuration.timeoutIntervalForResource = 180
let alamoFireManager = Alamofire.SessionManager(configuration: configuration)
return alamoFireManager
}()