Created
June 12, 2020 09:03
-
-
Save stevencurtis/488911fbc14fc5a6b1305ef9bed55799 to your computer and use it in GitHub Desktop.
UserListDecodeOperation
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
class UserListDecodeOperation: Operation { | |
var dataFetched: Data? | |
var error: Error? | |
var decodedURL: URL? | |
typealias CompletionHandler = (_ result: ListUsersModel?) -> Void | |
var completionHandler: (CompletionHandler)? | |
override func main() { | |
guard let dataFetched = dataFetched else { return } | |
let decoder = JSONDecoder() | |
do { | |
let content = try decoder.decode(ListUsersModel.self, from: dataFetched) | |
if let id = content.data.first?.id { | |
self.decodedURL = URL(string: Constants.baseURL + "/" + String(id) ) | |
completionHandler?(content) | |
} else { | |
self.error = OQError.runtimeError("Unexpected Data") | |
completionHandler?(nil) | |
} | |
} catch { | |
self.error = error | |
completionHandler?(nil) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment