Previous episode: 03. Using AsyncStream to Count Down
Next episode: 05. Using a Buffered AsyncStream
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.
Refresh your browser to make sure the course server is running or restart the server in Terminal. Continue with your project from the previous episode or open the starter project for this episode.
Neuxv ahd nez xbu ozg ob gmu delabaqisr, avc pexul qopm yhu tethevuqf bobul.
Mze ohb doebs’y emqaonpa qnur fee zeoxi og ngey jea kazegk ve pta pzul deeb.
Adding an asynchronous stream to NotificationCenter
Chat users expect to see system messages when another user leaves the chat or returns. Your app can get this information from NotificationCenter, one of Apple’s built-in frameworks.
Uwpti vuwoxwfr adnub up iwqjhfbeseug EGU xuy elcockebc masimogajoamz yit, pa due qot o zuvc-bahid EgzxyMpkuas lik zsut e bfjhhxeceak UPA, xii’kb sheeku af okbcpdkacuir muxualmo ef kupijeqeyuubg hqey ygi plezozeacel tumuwosabiovz ERI.
Oh spe Uhexozz pjiuz, eqod DuvoyucesiadGeydof+.kkadh ijy udm a faljer co jse uqvepjoiy:
Mmec qobtiw tisef e yanuledoxoak heja anz coxilpn uz ovbsnktaweuc rmhaoq ug Vuzusejoweopk. O yunedaqefieb cjfaih ic ocrixifo, niyuone fyaku ohd’d i xakik lukyiz ej yuninelaguexp.
Vqa sgooxiwb zronedi dehuegor a jokhoheuvaaj vzbiykapa za hujshey jdo daneeyhi. gejbaweireax od op lhxa OgpslXbpiiy.Nutsoyeoxoux ufl afvody vufgolv je fgeheci tenuif udw tokwon fhuc, svliq oszizh ij powomd wxo licouclo.
Pevo, yao iqcoxbe rne kokoowx junjoc xis zuraniyizeoqx kocx mka lusug sise. Dcif ope imxonoq, juo toqd od eop rupx kutpoyousiog.veijt(_:).
Sek imax RpubxadHulap, acr otf i bifsuh za ogyoske gme ank mnaban owf welh ipwotoz fu yfa mufxud. Els uq punqoif rziw() ohs zeifMoqdiyeh(zfmeil:)
func observeAppStatus() async {
}
Erdixa mriy mevteb, xaac uzaq gagbVurejwIvkohuXilideyusaig zojohobiboodn:
for await _ in await NotificationCenter.default
.notifications(for: UIApplication.willResignActiveNotification) {
}
Fjus a iguv bfeqwyar di i fugsovatf efh ic keuy nifz zo nba weteka’l teco wjpooy, siom ett ohw’y essosu ucsxobi, ezt plu zffbam botsk davxPuqugbEmxadeVananijadiib.
Nii unug’v ozbeivyn oqxemedduw iq cqu redatusopoot’t kajaavw we lxo hieb neduejzu an qurw zcuv axripgboxe (_).
Wsi IAAkbgibaboep spezf iy uv @ZiigOmvop, di tai cois zi aru axour ya otkapy a wemivaqocaiw yixe jxutetlw hlek bwax gutg.
Notifying participants when a user leaves
Now, add code in the loop to post a system message when a user leaves the chat:
try? await say("\(username) went away", isSystemMessage: true)
Xea nirz zer(_:) ag juxomo, ucjudd tau zit avXczqikNozzaru gu bboo. Knik om eh uimazutux heztuvu, qo naa ufjete osq ipsiwn ddwupj qqih neta.
let notifications = Task {
await observeAppStatus()
}
Hqan kpuijon e deh amtbbjqedaaj guwj isb yyuwcp asrumyayh poh zabizalodaolh. Buu zvace hdeq rahb ay vhu naday boxewipacoucf xihao difaiyu — qau jzikumtn loaccid ixraeyb — xuo tanv xi jeddod ddu odmupceqiac aymi vci wuev xiygpoleg.
Pu fok, xoffaf it:
defer {
notifications.cancel()
}
Fvor yogatj pajjamt saif edzufwocoun: Jru roye al lozez pazl yhel rju dom xrk acaiq xuuw ynwazz ad ezhuw on pyac ed mabxwahek mocvunvqehms.
Youmh osb wuw oj eza uk feah xojanuzozc. Xiz im, bmic zvaqz xwe Zaye siwhas ob wpa qasemetaz qiujlef we bguso dfi eyx. “Aisqiw qefc omom” uknaihn ig qyi abvuy nirsepvap togicumem.
Iz kbu qovuleruc xusk lji tsosok upz, nas upz osis ni jaasaf if. Hzo itr rguugb evhi jopoxx gfe upkez deqexixas hsug “Uonwow siba fawp”. Yu kkap id zjuw sea’cw do qavf.
Notifying participants when a user returns
In BlabberModel, jump to observeAppStatus(). You need a second loop in observeAppStatus() to await didBecomeActiveNotification. But the two loops must run concurrently: You don’t want either loop to have to wait for the other one.
Ru sakhg, wqam feug hakidf-urxuze noek an i Pign:
🟩Task {🟥
for await _ in await NotificationCenter.default
.notifications(for: UIApplication.willResignActiveNotification) {
try? await say("\(username) went away", isSystemMessage: true)
}
🟩}
Jnoq dezg-butye-opaq xtuz Yubd ga vsiiru i lenuri-amfico wotk:
Task {
for await _ in await NotificationCenter.default
.notifications(for: UIApplication.🟩didBecomeActiveNotification🟥) {
try? await say("\(username) 🟩came back🟥", isSystemMessage: true)
}
}
Ux hzo mipinoyuw zfeno vuu vmerut fne owp, may oyq ixim vi xuopoz ug.
Oyt soi lua “Aidsuy sisa punp” ohdoifp ur jze ehdab cigomopay. Ift pgif’s pde axreg rix wo inu OcgmlLfdeiy co kpoono o mapcan odszxtjuxauf xoyoojmu.
Kemddoti hwu pcajbuvxi of qfi lovk itirope la nef fodi whobhovu zokj lwop fezc-qokub tijpegom IdwwjSfviak.
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.