Skip to content

Instantly share code, notes, and snippets.

@StanislavK
Created April 4, 2020 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StanislavK/867731ca743cd1458aa0740617bbd452 to your computer and use it in GitHub Desktop.
Save StanislavK/867731ca743cd1458aa0740617bbd452 to your computer and use it in GitHub Desktop.
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