Reputation: 475
I'm getting this crash with Alamofire 5.0 CR3
Crash occurs in RequestTaskMap.swift from Alamofire library.
I do not have any stack trace, only a Firebase crash report, which I find it kinda useless.
I get this crash some of the time, on the same method, with same value.
func getUserDetails(for id: Int, success: @escaping(User) -> (), fail: @escaping failBlock) {
APIClient.performRequest(for: UserDetailsRoute(userId: id)) { (response: Result<User, AFError>) in
self.complete(response, success: success, fail: fail)
}
}
Router:
struct UserDetailsRoute: Route {
let userId: Int
var methodType: HTTPMethod { return .get }
var path: URL { return APIType.client.url.appendingPathComponent("/user/\(userId)") }
var customHeaders: [String : String]? { return [HTTPHeaderField.contentType.rawValue : ContentType.urlEncoded.rawValue] }
}
PerformRequest:
@discardableResult
func performRequest<T: Decodable>(for route: Route, decoder: JSONDecoder = JSONDecoder(), completion: @escaping (Result<T, AFError>) -> Void) -> DataRequest {
return sessionManager.request(NetworkRouter(route))
.validate(statusCode: 200..<300)
.responseDecodable(decoder: decoder) { (response: DataResponse<T, AFError>) in
completion(response.result)
}
}
URLRequestConvertible:
struct NetworkRouter: URLRequestConvertible {
let route: Route
init(_ route: Route) {
self.route = route
}
func asURLRequest() throws -> URLRequest {
var urlRequest = URLRequest(url: route.path)
// HTTP Method
urlRequest.httpMethod = route.methodType.rawValue
// Common Headers
urlRequest.setValue(NetworkConfig.CLIENT_ID, forHTTPHeaderField: HTTPHeaderField.clientId.rawValue)
urlRequest.setValue(AppSettings.shared.interfaceLanguage, forHTTPHeaderField: HTTPHeaderField.acceptLanguage.rawValue)
// Custom headers
if let headers = route.customHeaders {
for header in headers {
urlRequest.setValue(header.value, forHTTPHeaderField: header.key)
}
}
// Body
if let route = route as? RouteBody {
urlRequest.httpBody = route.data
}
// Parameters
if let route = route as? RouteParameters {
do {
urlRequest = try route.encoder.encode(urlRequest, with: route.parameters)
} catch {
throw AFError.parameterEncodingFailed(reason: .customEncodingFailed(error: error))
}
}
return urlRequest
}
}
Any idea what can I do to fix this? I really dunno where to start.
// Edit 1: Adding Zombi Objects in AppScheme gives me: Fatal error: Unexpectedly found nil while unwrapping an Optional value.
Hmm
// Edit 3: Adding stack trace from bt command:
Upvotes: 1
Views: 1722
Reputation: 162
It sees to be a multi-threading issue. Are you using custom queues when creating the Session? Check if rootQueue is serial.
According to doc:
/// Root `DispatchQueue` for all internal callbacks and state update. **MUST** be a serial queue.
Upvotes: 2