Created
April 4, 2020 17:50
-
-
Save StanislavK/867731ca743cd1458aa0740617bbd452 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Data { | |
func decoded<T: Decodable>( | |
as type: T.Type = T.self, | |
handledOn resultQueue: DispatchQueue = .main, | |
handler: @escaping (Result<T, Error>) -> Void | |
) { | |
let queue = DispatchQueue(label: "com.myapp.decoding") | |
let decoder = JSONDecoder() | |
queue.async { | |
let result = Result { | |
try decoder.decode(T.self, from: self) | |
} | |
resultQueue.async { handler(result) } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment