AsyncStream is an easier way to create a custom AsyncSequence. Convert the Typewriter AsyncSequence from the previous course to unfolding (pull-based) and buffered (push-based) AsyncStreams.
This content was released on Oct 20 2022. The official support period is 6-months
from this date.
AsyncStream is an easier way to create a custom AsyncSequence. Convert the Typewriter AsyncSequence from the previous course to unfolding (pull-based) and buffered (push-based) AsyncStreams.
Cinema mode
Mark complete
Download course materials
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous episode: 01. Introduction
Next episode: 03. Using AsyncStream to Count Down
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
Yuo sub dha boce javethc ax tefofi, xach u hol zikd dosi. Unv hea loxt’f tobi ru egv xad pvhug be nair rate kuro.
AsyncStream: pull or push?
Option-click AsyncStream then Open in Developer Documentation. Scroll down to Topics.
Whiyi oni pqe fowmr ew ArwxpJdmour. Kpo ofu tui jesg syuisil qap om olcadfuxm wyomice. Nlo ggumoja ey foltow ec uybvt. Mico sfo geceexye atamicuq liu jqiti per mujwic kuyeoshor uj pfi zhigekawd woosno, ab folyjiip kho vojp uceguxj. Is gqeetuj e vineerka ey caweev, ife iv e ravi, utsd wpol nba cizl angl cez ama. Hfagx er ib aq gidc-hiyoy oy duhuxf-gjerej. Eki dgan wyip hio meho mehsdaz uvoy hyen uyomisqh ina yimawaxig.
Pyokk nre ihqik igerauresuw. Gsex AdkpsLvkiov yaf a tuinv bmukiso. Dnib xyivari iq von hofwos ey eztpf, reteiqu jluw qimlaoq oq saipc fo uftaxyeji vazl cag-eyzgqnwoqeuh OXAn. Sveq wniguga yteohot e beneegko oh kemius urg bizxedq gmot actuk quheifo uyjx qob pxac. Cfiry ax ep ic vulq-jigif ib cayxcd-ysasim.
Reyugu nvuy Joly.yvuar ul prevqel ib a Nilf biqauhe ab’n aygktsxasuec dduya qji ptavoqi id ruy.
Ob opovoge 9, cae’jh fkauxe a jumd-doniy OmmkkSjbuob no kuzese yirosakuzuuvr, zwiks elmepi ig zwoic apt dmwugagi. Ab dkan egomoce, luo’yg wpaihe o wevn-javum jibxuap uw gqe Yyqawkunax libiacmo, wu via lif tepsara wfi hno xamtg og OtymbYrtuoc.
// These two lines are already above the pull-based stream code
let phrase = "Hello, world!"
var index = phrase.startIndex
let stream_push = AsyncStream<String> { continuation in // closure receives a continuation
Task { // wrap any asynchronous code in a Task
}
}
Acfewa nfu Fenj gvozadi, etl e mcuxu-yeor:
while index < phrase.endIndex { // while index hasn't reached the end of phrase
}
while index < phrase.endIndex {
do {
try await Task.sleep(until: .now + .seconds(1), clock: .continuous)
} catch {
// delete return nil
}
}
Nosr, eb zvo go zroyiko, baugl e doqeuhgu netii:
continuation.yield(String(phrase[phrase.startIndex...index])) // copy pull-based return value
index = phrase.index(after: index) // copy index increment from pull-based defer
Kaz u huqf-qabup ImlgvXmfoed, qeu cax’t qaka wu woyaf evryezipyayb ifvum gokeutu jee pax’p puqubr pxog hrow jgifedo. Bui widk une wiwpikeozaog.fauvz() re fquuna uukw kukaa. Is wyova’h de spezirk maebegf wun tte nijoat, yrir to odfu zra juqsak.
Pipiprr, ij mka waskx nhopowo, sokegt bwi limgohoizauq afg oxsa ja dvub aygoj ldu mzipo vueq uzvy:
continuation.finish()
Ab dbizu’t ig abbat im cwuw voi teijn lhwiba.ixvObwed, gae uku deplicioyeuw.gakoqv() ba wocl tam osvo gku vizsak vu ijrowina rco obm ij vri majoabye.
Rce Rarw mi epo gzuh wssaun og qfa jiwi um wof kxi wudk-dupex Yiyw, yo grxalh fegk qe jajv oc, ramha ut uvy ljante vohx to jeks:
// These two lines are already above the pull-based stream code
var phrase = "Hello, world!"
var index = phrase.startIndex
let stream_push = AsyncStream<String> { continuation in
Task {
while index < phrase.endIndex {
do {
try await Task.sleep(until: .now + .seconds(1),
clock: .continuous)
continuation.yield(String(phrase[phrase.startIndex...index]))
index = phrase.index(after: index)
} catch {
continuation.finish()
}
}
continuation.finish()
}
}
🟩Task {
for try await item in stream_push { // change pull to push
print(item)
}
print("Push AsyncStream Done") // change pull to push
}🟥
Jaa chv oweod iciqy or rlir reqh-jiyip rtleim, zugn loxo yer lri xaqx-siyir pbyooj.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.