This file contains hidden or 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
| let url = URL(string:"https://www.sebastianboldt.com") | |
| let pollingSequence = URLPollingSequence(url: url!, delay: 2) | |
| do { | |
| for try await item in pollingSequence { | |
| guard let data = item else { | |
| continue | |
| } | |
| print(String(data: data, encoding: String.Encoding.utf8) ?? "Not decodable") | |
| } | |
| } catch { |
This file contains hidden or 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
| struct ContentView: View { | |
| private let formatter = { | |
| let formatter = DateFormatter() | |
| formatter.dateStyle = .none | |
| formatter.timeStyle = .medium | |
| return formatter | |
| }() | |
| private let timerSequence = Timer.publish(every: 1, | |
| tolerance: 1, |
This file contains hidden or 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
| // Will immediatly start running after creation | |
| let task = Task { () -> String in | |
| let text = try await requestTextFromServer() | |
| return text | |
| } |
This file contains hidden or 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
| let task = Task { () -> String in | |
| let text = try await requestTextFromServer() | |
| return text | |
| } | |
| let newValue = try await task.value // Will only return if the Task did not throw |
This file contains hidden or 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
| let task = Task { () -> String in | |
| let text = try await requestTextFromServer() | |
| return text | |
| } | |
| let result = try await task.result | |
| switch result { | |
| case let .success(value): | |
| print(value) | |
| case let .failure(error): |
This file contains hidden or 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
| let task = Task(priority: .medium) { () -> String in | |
| let text = try await requestTextFromServer() | |
| return text | |
| } |
This file contains hidden or 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
| func getUser() async throws -> User { | |
| try await Task.sleep(nanoseconds: 1_000_000) | |
| let task = Task(priority: .high) { () -> Address in | |
| try await Task.sleep(nanoseconds: 1_000_000) | |
| return Address(street: "FooStreet") | |
| } | |
| let address = try await task.value | |
| return User(name: "Max", address: address) | |
| } |
This file contains hidden or 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
| let task = Task { | |
| for i in 0..<1_000_000_000_000_000 { | |
| try Task.checkCancellation() | |
| print(i) | |
| } | |
| } | |
| task.cancel() |
This file contains hidden or 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
| enum TaskData { | |
| @TaskLocal static var userName: String? | |
| } |
This file contains hidden or 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
| enum TaskData { | |
| @TaskLocal static var userId: Int? | |
| } | |
| Task { | |
| await TaskData.$userId.withValue(786) { | |
| let task = Task { | |
| print(TaskData.userId ?? "No User Id") | |
| } | |
| print(TaskData.userId ?? "No User Id") |