0

I am using Alamofire in my project but sometimes I have seen some non replicable crash in alamofire session manager in alamofire implementation file. As because it is non replicable causes some random crashes in my application.

After lots of debugging I found one log that it might a network issue. If anyone can find anything same please help to solve this. Thanks in advance

open func makeRequest(manager: SessionManager, method: HTTPMethod, encoding: ParameterEncoding, headers: [String:String]) -> DataRequest {

    return manager.request(URLString, method: method, parameters: parameters, encoding: encoding, headers: headers)
}

override open func execute(_ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) {
    let managerId:String = UUID().uuidString
    // Create a new manager for each request to customize its request header
    let manager = createSessionManager()
    managerStore[managerId] = manager

    let encoding:ParameterEncoding = isBody ? JSONDataEncoding() : URLEncoding()

    let xMethod = Alamofire.HTTPMethod(rawValue: method)
    let fileKeys = parameters == nil ? [] : parameters!.filter { $1 is NSURL }
                                                       .map { $0.0 }
     // the status code
    if fileKeys.count > 0 {
        manager.upload(multipartFormData: { mpForm in
            for (k, v) in self.parameters! {
                switch v {
                case let fileURL as URL:
                    if let mimeType = self.contentTypeForFormPart(fileURL: fileURL) {
                        mpForm.append(fileURL, withName: k, fileName: fileURL.lastPathComponent, mimeType: mimeType)
                    }
                    else {
                        mpForm.append(fileURL, withName: k)
                    }
                case let string as String:
                    mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
                case let number as NSNumber:
                    mpForm.append(number.stringValue.data(using: String.Encoding.utf8)!, withName: k)
                default:
                    fatalError("Unprocessable value \(v) with key \(k)")
                }
            }
            }, to: URLString, method: xMethod!, headers: nil, encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                if let onProgressReady = self.onProgressReady {
                    onProgressReady(upload.uploadProgress)
                }
                self.processRequest(request: upload, managerId, completion)
            case .failure(let encodingError):
                var statusCode = encodingResult
                if let error = encodingError as? AFError {
                  //  statusCode = error._code // statusCode private
                    switch error {
                    case .invalidURL(let url):
                        print("Invalid URL: \(url) - \(error.localizedDescription)")
                    case .parameterEncodingFailed(let reason):
                        print("Parameter encoding failed: \(error.localizedDescription)")
                        print("Failure Reason: \(reason)")
                    case .multipartEncodingFailed(let reason):
                        print("Multipart encoding failed: \(error.localizedDescription)")
                        print("Failure Reason: \(reason)")
                    case .responseValidationFailed(let reason):
                        print("Response validation failed: \(error.localizedDescription)")
                        print("Failure Reason: \(reason)")

                        switch reason {
                        case .dataFileNil, .dataFileReadFailed:
                            print("Downloaded file could not be read")
                        case .missingContentType(let acceptableContentTypes):
                            print("Content Type Missing: \(acceptableContentTypes)")
                        case .unacceptableContentType(let acceptableContentTypes, let responseContentType):
                            print("Response content type: \(responseContentType) was unacceptable: \(acceptableContentTypes)")
                        case .unacceptableStatusCode(let code):
                            print("Response status code was unacceptable: \(code)")
                           // statusCode = code
                        }
                    case .responseSerializationFailed(let reason):
                        print("Response serialization failed: \(error.localizedDescription)")
                        print("Failure Reason: \(reason)")
                        // statusCode = 3840 ???? maybe..
                    }

                    print("Underlying error: \(error.underlyingError)")
                } else if let error = encodingError as? AFError {
                    print("URLError occurred: \(error)")
                } else {
                    print("Unknown error: \(encodingError as? AFError)")
                }

                print(statusCode)
                completion(nil, ErrorResponse.error(415, nil, encodingError))
            }
        })
    } else {
        let request = makeRequest(manager: manager, method: xMethod!, encoding: encoding, headers: headers)

        if let onProgressReady = self.onProgressReady {
            onProgressReady(request.progress)
        }
        processRequest(request: request, managerId, completion)
    }

}

EXC_BAD_ACCESS

4

0 回答 0