-
Meet Swift Async Algorithms
Discover the latest open source Swift package from Apple: Swift Async Algorithms. We'll explore algorithms from this package that you can use with AsyncSequence, including zip, merge, and throttle. Follow along with us as we use these algorithms to build a great messaging app. We'll also share best practices for combining multiple AsyncSequences and using the Swift Clock type to work with values over time.
To get the most out of this session, we recommend watching "Meet AsyncSequence."Recursos
Videos relacionados
WWDC22
WWDC21
-
Buscar este video…
-
-
2:01 - The messaging app
struct Account { var messages: AsyncStream<Message> } actor AccountManager { var primaryAccount: Account var secondaryAccount: Account? } protocol MessagePreview { func displayPreviews(_ manager: AccountManager) async } -
3:16 - Zip
// upload attachments of videos and previews such that every video has a preview that are created concurrently so that neither blocks each other. for try await (vid, preview) in zip(videos, previews) { try await upload(vid, preview) } -
5:09 - Merge
// Display previews of messages from either the primary or secondary account for try await message in merge(primaryAccount.messages, secondaryAccount.messages) { displayPreview(message) } -
6:37 - Suspending Clock
// Sleep until a given deadline let clock = SuspendingClock() var deadline = clock.now + .seconds(3) try await clock.sleep(until: deadline) -
6:56 - Suspending Clock vs. Continuous Clock
let clock = SuspendingClock() let elapsed = await clock.measure { await someLongRunningWork() } //Elapsed time reads 00:05.40 let clock = ContinuousClock() let elapsed = await clock.measure { await someLongRunningWork() } //Elapsed time reads 00:19.54 -
8:34 - Control searching messages
// Control searching messages class SearchController { let searchResults = AsyncChannel<SearchResult>() func search<SearchValues: AsyncSequence>(_ searchValues: SearchValues) where SearchValues.Element == String } -
9:16 - Debounce
let queries = searchValues .debounce(for: .milliseconds(300)) for await query in queries { let results = try await performSearch(query) await channel.send(results) } -
10:21 - Chunked by
let batches = outboundMessages.chunked( by: .repeating(every: .milliseconds(500)) ) let encoder = JSONEncoder() for await batch in batches { let data = try encoder.encode(batch) try await postToServer(data) } -
11:22 - Conversions in initializers
// Create a message with awaiting attachments to be encoded init<Attachments: AsyncSequence>(_ attachments: Attachments) async rethrows { self.attachments = try await Array(attachments) }
-