Last active
August 2, 2023 13:23
-
-
Save MarcoSantarossa/406652eeb00d3281980cba5fc21c5ff6 to your computer and use it in GitHub Desktop.
How to write and use an asynchronous operation
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
import Foundation | |
class AsyncOperation: Operation { | |
enum State: String { | |
case isReady, isExecuting, isFinished | |
} | |
override var isAsynchronous: Bool { | |
return true | |
} | |
var state = State.isReady { | |
willSet { | |
willChangeValue(forKey: state.rawValue) | |
willChangeValue(forKey: newValue.rawValue) | |
} | |
didSet { | |
didChangeValue(forKey: oldValue.rawValue) | |
didChangeValue(forKey: state.rawValue) | |
} | |
} | |
override var isExecuting: Bool { | |
return state == .isExecuting | |
} | |
override var isFinished: Bool { | |
return state == .isFinished | |
} | |
override func start() { | |
guard !self.isCancelled else { | |
state = .isFinished | |
return | |
} | |
state = .isExecuting | |
main() | |
} | |
} |
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
final class FetchOperation: AsyncOperation { | |
private(set) var dataFetched: Data? | |
private let session = URLSession(configuration: .default) | |
private var dataTask: URLSessionDataTask? | |
override func main() { | |
let url = URL(string: "https://jsonplaceholder.typicode.com/users")! | |
dataTask = session.dataTask(with: url) { [weak self] (data, _, _) in | |
guard let `self` = self else { return } | |
// We could improve this fallback with a Result enum | |
guard | |
!self.isCancelled, | |
let data = data else { | |
self.state = .isFinished | |
return | |
} | |
self.dataFetched = data | |
self.state = .isFinished | |
} | |
dataTask?.resume() | |
} | |
} |
@robwithhair/@badrinathvm so far I didn't encounter thread problems. I should investigate about this issue.
Looking around, I think we could use an internal GCD queue to update the state with a sync work. I will update this code asap.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yea facing same issue.