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
final class ContactsService { | |
let userAPI = UserAPI() | |
func fetchUser() async throws { | |
try await userAPI.fetchAllUsersInContacts() | |
} | |
} |
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
actor TaskActor { | |
private var cachedTask: Task<Void, Error>? | |
func run(_ operation: @escaping @Sendable () async throws -> Void) async throws { | |
if cachedTask == nil { | |
cachedTask = Task { | |
try await operation() | |
} | |
} |
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
final class ContactsService { | |
let userAPI = UserAPI() | |
private(set) var contacts = [Contact]() | |
func fetchContacts() async throws { | |
contacts = try await userAPI.fetchAllUsersInContacts() | |
} | |
} |
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
actor TaskActor<T> { | |
private var cachedTask: Task<T, Error>? | |
func run(_ operation: @escaping @Sendable () async throws -> T) async throws -> T { | |
if cachedTask == nil { | |
cachedTask = Task { | |
try await operation() | |
} | |
} |
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
final class ContactsService { | |
let userAPI = UserAPI() | |
private(set) var contacts = [Contact]() | |
private let contactsActor = TaskActor<[Contact]>() | |
func fetchContacts() async throws { | |
contacts = try await contactsActor.run { [weak self] in | |
try await self?.userAPI.fetchAllUsersInContacts() ?? [] | |
} | |
} |
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
.onTapGesture { | |
showButton = true | |
hideSkipButtonTask = Task { | |
try await Task.sleep(for: .seconds(2)) | |
showButton = false | |
} | |
} |
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
.onTapGesture { | |
showButton = true | |
hideSkipButtonTask?.cancel() | |
hideSkipButtonTask = Task { | |
try await Task.sleep(for: .seconds(2)) | |
try Task.checkCancellation() | |
showButton = false | |
} | |
} |
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
.onTapGesture { | |
showButton = true | |
hideSkipButtonTask?.cancel() | |
hideSkipButtonTask = Task { | |
try await Task.sleep(for: .seconds(2)) | |
showButton = false | |
} | |
} |
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
@State private var showContinueButton = false | |
var body: some View { | |
// ... | |
.task { | |
try? await Task.sleep(for: .seconds(12)) | |
showContinueButton = true | |
} |
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
class PollingManager { | |
private var tasks: [UUID: Task<Double, Never>] = [:] | |
func uploadDataAndReturnCash(for categories: [Category]) async -> Double? { | |
try? await uploadDataToBackend(categories) | |
for category in categories { | |
let taskID = UUID() | |
let task = Task.detached { [weak self] in | |
return (try? await self?.pollBackend(for: category)) ?? 0.0 | |
} |