In my app, I'm using Alamofire as network library. I have an object like this (all properties are non-optional)
struct User: Codable {
let name: String
let email: String
let role: String
}
In the network I have a function that invoke an API HTTP to fetch User
as array of users:
.responseDecodable(of: [User].self)
the problem is: is possible to find inside the database some user that was created in a bad way without all mandatory fields, so the Decodable
fails. I expected to have an array without this broken user, instead all the decode fails anche the request fails with an error like
responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.valueNotFound(Swift.Int, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "role", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "role", intValue: nil), CodingKeys(stringValue: "time", intValue: nil)], debugDescription: "Expected String value but found null instead.", underlyingError: nil))))
Is there a way to use the power of responseDecodable
without having this error and simply discard the broken user?
Thanks in advance.