Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Adding DateFormat into param
- func run<T: Decodable>(_ request: URLRequest, _ decoder: JSONDecoder = JSONDecoder(), dateFormat: String? = nil) -> AnyPublisher<Response<T>, Error> {
- return URLSession.shared
- .dataTaskPublisher(for: request)
- .receive(on: DispatchQueue.main)
- .tryMap { result -> Response<T> in
- self.dateFormatter.dateFormat = dateFormat ?? "yyyy-MM-dd'T'HH:mm:ss"
- decoder.dateDecodingStrategy = .formatted(self.dateFormatter)
- let value = try decoder.decode(T.self, from: result.data)
- return Response(value: value, response: result.response)
- }
- .eraseToAnyPublisher()
- }
- ---
- Same idea, but with better handle: using a enum which will have "safe values"
- enum CustomDateFormat: String {
- case format1 = "yyyy-MM-dd'T'HH:mm:ss"
- case format2 = "yyyy-MM-dd'T'HH:mm:ssZ"
- }
- func run<T: Decodable>(_ request: URLRequest, _ decoder: JSONDecoder = JSONDecoder(), dateFormat: CustomDateFormat = .format1) -> AnyPublisher<Response<T>, Error> {
- return URLSession.shared
- .dataTaskPublisher(for: request)
- .receive(on: DispatchQueue.main)
- .tryMap { result -> Response<T> in
- self.dateFormatter.dateFormat = dateFormat.rawValue
- decoder.dateDecodingStrategy = .formatted(self.dateFormatter)
- let value = try decoder.decode(T.self, from: result.data)
- return Response(value: value, response: result.response)
- }
- .eraseToAnyPublisher()
- }
- ----
- //Extension to avoid self.dateFormatter which I guess isn't use elsewhere
- // Can be used as private static of your class if needed
- extension DateFormatter {
- static var formatter1: DateFormatter {
- //... with Format1
- }
- static var formatter2: DateFormatter {
- //... with Format2
- }
- }
- func run<T: Decodable>(_ request: URLRequest, _ decoder: JSONDecoder = JSONDecoder(), dateFormatter: DateFormatter = .formatter1) -> AnyPublisher<Response<T>, Error> {
- return URLSession.shared
- .dataTaskPublisher(for: request)
- .receive(on: DispatchQueue.main)
- .tryMap { result -> Response<T> in
- decoder.dateDecodingStrategy = .formatted(dateFormatter)
- let value = try decoder.decode(T.self, from: result.data)
- return Response(value: value, response: result.response)
- }
- .eraseToAnyPublisher()
- }
- ----
- //Instead, use the correct JSONDecoder parameter in your current code.
- extension JSONDecoder {
- static var defaultDecoder: JSONDecoder {
- let decoder = JSONDecoder()
- let dateFormatter = //Create/retrieve DateFormatter format1
- decoder.dateDecodingStrategy = .formatted(dateFormatter)
- }
- static var otherDecoder: JSONDecoder {
- let decoder = JSONDecoder()
- let dateFormatter = //Create/retrieve DateFormatter format2
- decoder.dateDecodingStrategy = .formatted(dateFormatter)
- }
- }
- with
- func run<T: Decodable>(_ request: URLRequest, _ decoder: JSONDecoder = JSONDecoder()) -> AnyPublisher<Response<T>, Error> {
- return URLSession.shared
- .dataTaskPublisher(for: request)
- .receive(on: DispatchQueue.main)
- .tryMap { result -> Response<T> in
- let value = try decoder.decode(T.self, from: result.data)
- return Response(value: value, response: result.response)
- }
- .eraseToAnyPublisher()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement