Ankit Srivastava
Ankit Srivastava

Reputation: 12405

Alamofire stops working after some requests in Swift 3.0 (Alamofire 4.4.0)

Key info:

  1. Projected migrated from 2.3 to Swift 3.0.
  2. Xcode version 8.2.1, iOS version 10.2, Alamaofire 4.4.0, AlamofireObjectMapper 4.1.0

Alamofire stops making calls after certain calls are made. I do not receive a callback and I cannot see any network calls being made in Charles once it stops working.

I make calls using a Manager which in turn calls a helper client Class which requests Alamofire for a request object which I store in an array in client. You can see some part of the code of my classes below.

// This is in BaseClient
    /* GET Request
     default response with mappable data and NSerror
     */
    @discardableResult
    class func getRequest<T: Mappable>(_ apiRoute: String, params: [String: Any], credentialsRequired: Bool = false, completion: @escaping (T?, Error?) -> Void) -> DataRequest {
        let headers = additionalHeaders(credentialsRequired)
        let request = Alamofire.request(getURL(for: apiRoute), method: .get, parameters: params, headers: headers)
            .responseObject { (response: DataResponse<DefaultResponse<T>>) -> Void in
                logResponse(response.result.value)
                if let error = self.checkForError(response, endPoint: apiRoute) {
                    completion(nil, error)
                } else {
                    completion(response.result.value?.data, nil)
                }
        }

        logCurl(request)
        return request
    }

This is the helper Client Class

    import ObjectMapper
    import Alamofire

    class PackageClient: BaseClient {
        fileprivate struct PackagesEndPoint {
            static let inspire = "/api/v2/something"
    }

fileprivate static var currentRequests = [Request]()

class func cancelAllFetchRequests() {
    for item in currentRequests {
        item.cancel()
    }
    currentRequests = [Request]()
}

    class func fetchInspire(_ completion: @escaping (_ response: InspireResponse?, _ error: Error?) -> Void) {
PackageClient.cancelAllFetchRequests()

        var params = [String: Any]()
        params["product"] = "flight,event,package" 
      let request = getRequest(PackagesEndPoint.inspire,
                   params: params,
                   credentialsRequired: false){ (response: InspireResponse?, error: Error?) in
                    completion(response, error)
        }
    currentRequests.append(request)
    }
}

This is in PackageManager:

    PackageClient.fetchInspire { [weak self] (response, error) in

        if error == nil {
            self?.inspireResponse = response
        }
        self?.finishLoading(error: error)
    }

The problem is once Alamofire stops working, then a simple call like below also does not make any network calls... I can't see anything on charles and obviously no callback is returned.

Alamofire.request(URL(string: urlString), method: .get, parameters: parameters, encoding: JSONEncoding.default)
.validate { request, response, data in
    // Custom evaluation closure now includes data (allows you to parse data to dig out error messages if necessary)
    return .success
}
.responseJSON { response in
    debugPrint(response)
}

Any help is appreciated. I have spent 2 days on debugging this. :(

Upvotes: 1

Views: 648

Answers (1)

Sahil Kapoor
Sahil Kapoor

Reputation: 11773

This happened to me too. I was using objc_sync_enter(self) calls in my code which was causing deadlocks.

This issue of Alamofire on Github may help you.

Upvotes: 1

Related Questions