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
Task { | |
await MainActor.run { | |
// .... | |
} | |
} | |
// or | |
Task { @MainActor in | |
// ..... |
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
let viewState = await MainActor.run { () -> ViewState in | |
// .... | |
} |
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
func loadData() async -> Data { | |
return Data() | |
} | |
func refreshUI(with data: Data) async { | |
let data = await loadData() | |
await MainActor.run { | |
view.update(with: data) | |
} | |
} |
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
actor BankAccount { | |
private let accountHolder: String? | |
// ..... | |
nonisolated func logAccountHolder() { | |
print(accountHolder ?? "No account holder") | |
} | |
} | |
Task { | |
let bankAccount = BankAccount(accountHolder: "Sebastian Boldt") |
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
actor BankAccount { | |
var balance: Int = 0 | |
let accountHolder: String? | |
init(accountHolder: String) { | |
self.accountHolder = accountHolder | |
} | |
func logBalance() { | |
print(balance) // Accessing Balance without await |
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
actor BankAccount { | |
private var balance: Int = 0 | |
let accountHolder: String? // 3. | |
init(accountHolder: String) { | |
self.accountHolder = accountHolder // 2. | |
} | |
func logBalance() { | |
print(balance) // Accessing Balance without await |
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
actor BankAccount {} |
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
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") |
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
enum TaskData { | |
@TaskLocal static var userName: String? | |
} |
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
let task = Task { | |
for i in 0..<1_000_000_000_000_000 { | |
try Task.checkCancellation() | |
print(i) | |
} | |
} | |
task.cancel() |
NewerOlder