Advertisement
Larme

Untitled

Aug 24th, 2020
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.42 KB | None | 0 0
  1. Adding DateFormat into param
  2.  
  3. func run<T: Decodable>(_ request: URLRequest, _ decoder: JSONDecoder = JSONDecoder(), dateFormat: String? = nil) -> AnyPublisher<Response<T>, Error> {
  4.     return URLSession.shared
  5.         .dataTaskPublisher(for: request)
  6.         .receive(on: DispatchQueue.main)
  7.         .tryMap { result -> Response<T> in
  8.             self.dateFormatter.dateFormat = dateFormat ?? "yyyy-MM-dd'T'HH:mm:ss"
  9.             decoder.dateDecodingStrategy = .formatted(self.dateFormatter)
  10.             let value = try decoder.decode(T.self, from: result.data)
  11.             return Response(value: value, response: result.response)
  12.         }
  13.         .eraseToAnyPublisher()
  14. }
  15. ---
  16.  
  17. Same idea, but with better handle: using a enum which will have "safe values"
  18. enum CustomDateFormat: String {
  19.     case format1 =  "yyyy-MM-dd'T'HH:mm:ss"
  20.     case format2 =  "yyyy-MM-dd'T'HH:mm:ssZ"
  21. }
  22.  
  23. func run<T: Decodable>(_ request: URLRequest, _ decoder: JSONDecoder = JSONDecoder(), dateFormat: CustomDateFormat = .format1) -> AnyPublisher<Response<T>, Error> {
  24.     return URLSession.shared
  25.         .dataTaskPublisher(for: request)
  26.         .receive(on: DispatchQueue.main)
  27.         .tryMap { result -> Response<T> in
  28.             self.dateFormatter.dateFormat = dateFormat.rawValue
  29.             decoder.dateDecodingStrategy = .formatted(self.dateFormatter)
  30.             let value = try decoder.decode(T.self, from: result.data)
  31.             return Response(value: value, response: result.response)
  32.         }
  33.         .eraseToAnyPublisher()
  34. }
  35.  
  36. ----
  37.  
  38. //Extension to avoid self.dateFormatter which I guess isn't use elsewhere
  39. // Can be used as private static of your class if needed
  40. extension DateFormatter {
  41.  
  42.     static var formatter1: DateFormatter {
  43.     //... with Format1
  44.     }
  45.    static var formatter2: DateFormatter {
  46.     //... with Format2
  47.     }
  48. }
  49.  
  50. func run<T: Decodable>(_ request: URLRequest, _ decoder: JSONDecoder = JSONDecoder(), dateFormatter: DateFormatter = .formatter1) -> AnyPublisher<Response<T>, Error> {
  51.     return URLSession.shared
  52.         .dataTaskPublisher(for: request)
  53.         .receive(on: DispatchQueue.main)
  54.         .tryMap { result -> Response<T> in
  55.             decoder.dateDecodingStrategy = .formatted(dateFormatter)
  56.             let value = try decoder.decode(T.self, from: result.data)
  57.             return Response(value: value, response: result.response)
  58.         }
  59.         .eraseToAnyPublisher()
  60. }
  61.  
  62. ----
  63.  
  64. //Instead, use the correct JSONDecoder parameter in your current code.
  65. extension JSONDecoder {
  66.     static var defaultDecoder: JSONDecoder {
  67.         let decoder = JSONDecoder()
  68.         let dateFormatter = //Create/retrieve DateFormatter format1
  69.         decoder.dateDecodingStrategy = .formatted(dateFormatter)
  70.     }
  71.     static var otherDecoder: JSONDecoder {
  72.         let decoder = JSONDecoder()
  73.         let dateFormatter = //Create/retrieve DateFormatter format2
  74.         decoder.dateDecodingStrategy = .formatted(dateFormatter)
  75.     }
  76. }
  77.  
  78. with
  79.  
  80. func run<T: Decodable>(_ request: URLRequest, _ decoder: JSONDecoder = JSONDecoder()) -> AnyPublisher<Response<T>, Error> {
  81.     return URLSession.shared
  82.         .dataTaskPublisher(for: request)
  83.         .receive(on: DispatchQueue.main)
  84.         .tryMap { result -> Response<T> in
  85.             let value = try decoder.decode(T.self, from: result.data)
  86.             return Response(value: value, response: result.response)
  87.         }
  88.         .eraseToAnyPublisher()
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement