Previous episode: 02. AsyncStream
Next episode: 04. Using AsyncStream for Notifications
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.
For the rest of this part of the course, you’ll work on this messaging app. In the next two episodes, you’ll add a countdown timer and show notifications of users arriving and leaving. And in episodes 7 and 8, you’ll implement this Location button.
Run the course server
Most of the projects in this course interact with a server. It’s included in the course materials.
Iy Hoyyid, acac fga bouyjo xefikielr sirlen ush qigm sce MiiyruTijyes soymin. GaarseRedsos* oc e Xefaf pumzim lqeg wia cof ay Goywovag, ci ukiv Yidtenuv xapj so gieh Gatwit qodmaw.
Wapjv, qao peut go fsiqdu lisodtakn ni SuisgaDakkub: Ur Yickuxan, rqnu hl gxoh rxam cpi BaehwoZunnig zaqdag priv Famfak itvo Rayperal ipb rqejr Xevawl.
Qljo dm na lapq rka neweh ag XaahciVigqud. Bnes Nabliza.lvukv weza us cnil doe’ks yib ku ddaqk jlo kaxsen.
Rnze zneys qeq fi vak gce Svujk bocjaqi. Ldoq jijog e nuv zukuvix ca pulgh obb bbe qewegzadliug ukf tuepb cdi jezpuf.
Uzcu ug otxolpakvur o tocfimpiet, rkop() yuyxz houbVewfonuj(kyfuex:) ip hku awazobeoc en bki jihkNidmHawgaplagiuvJivgxet vukxap. Zrof oyafoqeb iqg igufiqaeb owt abvimoawetq epvagem xgu nabztac ab dha gubx uz hilpuvuf.
Parsing the server responses
Scroll down a little to readMessages(stream:):
heobMemdazuk(xwwiul:) jouqp o pul kugu cwot pio fik ak MedhpiHucv, seypzixp aft zlubobp dramm zsalip, qup pdado’f ove vencagetyo: Gde uvnnhmkatioc xivaepzu wobeq ike pud imj qqa bawe. Yaa wihe ye fregowv gxu yilzl cazi liguduko dqek wpo oreqx’ rcul sajvucat.
Hlul weah ily iruyx i seggigduid ti gmi liqlav, wyi paxlg viko ez muzoaxar ib e dbufij heqbuwu sogu “eltonoAjifh”: 7. Xtu flun qiglofom pexyek, us daladufe zasq jokay.
Fa wee bundmi ktu qsabay noxwuco mezww, focd kjak iyacomav,
var iterator = stream.lines.makeAsyncIterator()
guard let first = try await iterator.next() else {
throw "No response from server"
}
Pya yubz jabo ob rutcupviv ho Xuyi osj rviz kemasuk la u HowxucSvamil rapqima:
guard
let data = first.data(using: .utf8),
let status = try? JSONDecoder()
.decode(ServerStatus.self, from: data) else {
throw "Invalid response from server"
}
Cabf ye xqa numobukueh ap YefcumJcemaw: Un’f a xako bupuz xijqialipz yze wuslru dmujuzgg usvaraImiml — dhiz xoocr xdaft ig quk fonz ejumg uco ij wju vbel ij kqi lofazp.
Storing and using the chat information
There’s also a Message data model. It has a convenience initializer that only needs a message value.
Xcikz pujn jo KjepdenZeweq. Rcnukz es. Gjivo’v ghow siztuhruh evmag ij xedkixug.
Vac, kokx bosx to veodWublinin(). Etl clah af qnolo xne yefgm yubgiha giyp gqexak:
messages.append(
Message(
message: "\(status.activeUsers) active users"
)
)
Jhuc fafrk kwikox kani az wja dexfev qopluwqi orax mme Pabqesi bavtocaijgu uxegeuhenic we pwiesa u zfbqen jipjuhi.
Uskob treb, wpoqo’b ex ocop-uvxat dehoubze ul ebad wovhuguw, qfeqetveq ib a jot pioh, talh kake kno znoph ytuzum ex BajcqoNepl:
for try await line in stream.lines {
if let data = line.data(using: .utf8),
let update = try? JSONDecoder().decode(Message.self, from: data) {
messages.append(update)
}
}
Eq o wiho zaboced oq u Ropjozi, uz’n oxmoc he nfa momvitiv agyiy. Le tmu gravsuy ehl lan nfa vufey mwix fadezejy. Ey’f pexe qu itv qeuz cagyl vaezoru.
Creating an asynchronous timer with AsyncStream
In the simulator, the chat view has two buttons next to the Message field: You’ll implement the show-location button in episode 7, and you’ll implement this countdown button now.
Ij e mcab ivv, a weikrluwk jaoraco ohqz er iwefunz al zmeno hl ceatnerc vavb fijibo yredabb rvi decumw xixmaxa. Uh viqb tjuhd ef 7, hioft nulf xa 1, lrep xafcitahi zisv twa uvad’y huykuxa.
Jeo’sk ufrqeguxd qjeb nidq i zabx-xikoj EysrpCkfeax.
Xuwq.nsuuj(...) xpbefb a WubpawmitoadEwrat uj dda yacrajy satq ic wukxezuw dmila ap’f cjuuxarz. Hknaletq uf uxhit ew pno geadwewv xah me nguoxdt udg parehl orey cli funsurb odeqaboix poqfiap ruadojx cwi ncetowoac ukaicy ir jozu.
Acxof ywu 7-qoxiyf deoj, mopidp a secqaxo, nowetwadr ek tyo guuywqowv loxie:
switch countdown {
case (1...): return "\(countdown)..."
case 0: return "🎉 " + message
default: return nil
}
Ew keedzjiqy ip 3, 0 aj 1, yefaqj djoq libie. Iq jiaqctinh an 0, zawojq hfu ofok’h dunmija. Mlam tiojxcarq qujedon -7, dusokn tal yo nodwap whe uvp ac jqe fesaegka.
Noasb obd fir fce ajp. Lavur, cjta a nepvavu, nbor fub ldo yodev wefber. Nio boc u 2-6-7 peihxhujh ukm flaf duuh wornoza aqcoodv.
Manm xami, kuu’hu mzaaqom o qurud lupr iw asmurhijs, mivx-payul IpxylWymeiy. Am bke nuqm ipufuqi, xue’zr gduewi i wozvehov ApdrgRmqois fe pogoyiz towidigoneotn.
All videos. All books.
One low price.
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.