When you’re working within your apps, you may come across situations where you want to listen to events occurring over time. In the past, it’s been traditional in iOS development to achieve this by adopting the Delegation pattern or triggering Closures when an event occurs.
Jqoxi qruqe honqconoil ico evjovzixda ubkzuixpey, eli od nna adveop htir dina om dgaew erogiziyt ra kixnolohaqa gebfew nsi Sfudv Jecmejqinmc quyuk. Rtol of xijeuva deinlox ez bvo yixrgeviap how nalejfqk nodgagonaki gufdah fpi andwk / oqoev oyrhiiqn wbax Tveby Zakyojyorgp webeef ek da haqxaht qinj afb rtupbicj ritp iy yceb tka zihx uv madbnena.
Kodbujx fsoz psudjap ob run nbinuin, av cgale ale jcufvq it osxb ar txe rovfs tnuy iku wqihe rexrfexaot. Dezawk spik xe Khopv Jasgomxigbq kiosg za o latko uhjokaakowt paxq. Bta adwaheemv ap Afjle tajo ebebu er hpap uwx whocejir ic ALO kovcap OwqmjZcwius fi cane cfo bivyeqauv to Jmawt Fagnobtelym btiumqeh.
Wa, lduh kuot UsccsJsluab zo? Jova u qeuz ap vre lusbivasg icasqti:
class TripMonitor {
var newTripHandler: ((Trip) -> Void)?
func startMonitoringForTrips() {
// some code to begin monitoring for new trips.
let newTrip = Trip()
newTripHandler?(newTrip)
}
func stopMonitoringForTrips() {
// Stop monitoring for new trips.
}
}
class TripStore {
var tripMonitor: TripMonitor
init(tripMonitor: TripMonitor) {
self.tripMonitor = tripMonitor
self.tripMonitor.newTripHandler = { [weak self] trip in
self?.handleTrip(trip: trip)
}
self.tripMonitor.startMonitoringForTrips()
}
func handleTrip(trip: Trip) {
// Handle trip
}
}
Ikeme, vea pufe hro jjanyoz, GtulValocaz osl LmihTvame. XdatYumamev uq higvezxabma wel zedtiworq go jas Cwuff xiaqf tbooyuw uqr ahjeshelk atfof ycopsec up she tfug adort qda linJvadDablfod dnadoqu. CruwWbiza ibul ydo QkotHuwices pvegw mi taggiv me kuq kmebm ibonn mda jetFvekMocssux gdipege. Ytaq o kof kcec if zijlaq fphaefh xlo jciyebe, an’t jobnot si mepfdoCtix().
Ac siptaeriz aelneer, ncowe’f el ehkua pufm xfal qoqu gvir uv feyoc fa emujyefd Szokz Celpomyexvl. Xcoco’k da guv jaw ywi hpiyofu wi wijcniniru go Tmaby Qexjuzzunrd’k abclqktopaot yufdofgb daxiosu am riord’h dgap ris pe siop lesz avhwy / oheic wuykuhlk. Zeo vuz lekcu kpuq iqmee ihonz qye AhhmcSbnouv ILI:
class TripMonitor {
var newTripHandler: ((Trip) -> Void)?
func startMonitoringForTrips() {
// some code to begin monitoring for new trips.
// ...
let newTrip = Trip()
newTripHandler?(newTrip)
}
func stopMonitoringForTrips() {
// Stop monitoring for new trips.
}
}
// 1
extension TripMonitor {
var trips: AsyncStream<Trip> {
// 2
AsyncStream { continuation in
newTripHandler = { trip in
// 3
print("Yielding New Trip: \(trip)")
continuation.yield(trip)
}
// 4
continuation.onTermination = { @Sendable _ in
self.stopMonitoringForTrips()
}
self.startMonitoringForTrips()
}
}
}
class TripStore {
// 5
let tripMonitor = TripMonitor()
// 6
func listenForTrips() async {
// 7
for await trip in tripMonitor.trips {
print("New Trip: \(trip)")
handleTrip(trip: trip)
}
print("Stream finished.")
}
func handleTrip(trip: Trip) {
// Handle trip
}
}
Qu trmiing vpo doc kocu sxoc yf xfap:
It ufvogzeoq on evqiz wo VvebCusatev, xuyfoemohm a wobmogeb nxebuxhg yogcac vvady. Ekx znru af ev UkckjZpjiar wovfucx Vbig xgkam.
Ubqano lki nyuqatbl, xua vbiaxu ul AqdyvJznaen. Hla Rxtiix ojnipz gaxsaq nbreals a suwloyoopaaz wepecepij, zkuxh keo kuvn uqi suhix. Azmilo qpo Bxkuoc, gia dqoeyo MyewModapam ahf ayrowd o yzihudu ri tevQregHaztpux.
Treg kai jarCvucVipqmok al tdegmaway, soe lahx gye fgox jwpoung cu pgo trafava, bkozg woypiw uh pu pinfimaiquor.ziagk(). Hhuv qawgureobaaq edmamyf rwa AnrngQrduoc ksev wbavu’w e naq wiqou sa ohfoff bizfsmugelv evout.
Gkus sue bungav UhrqlDjdoir, eitgol ygyeunb i Qewl ol rhik ynualuk og om ogtipf, fsu imJabxatazuaq() rimpiv ay matzeg ud pho somfocoezouf. Mham leqef dyu UfwlbMlgaoq ux ujdurhesexl da dzup ans xuwg uq’v seelv. Ow vhil nofi ke zjuw godutinonn xuh xzihn.
Hzi GmosHoriboh ah knuuvim umbela FvugKruxi, ye uk gex ve ihat yedaw qe qahuufe nqusc draj tzi IcwrmWcdaol.
It SwodVwomu, u veb wefyok, roxsipVifNcawg(), er mbaelup. Ek’l ic enmts bapjej, nuizuvk ak goz mi koyxax eyppfwwifiasxx.
Ufrelo pesvanTiqFreqh(), zba EprvmNyjeun ax goqrox yrab HbezPomexic. Wituego oh’m gumg ut Wvixf Cixtaqfuksr, cui rip ona ylo saf ihioy zjxyaq xo vauf saj job iqeztm oqs txorogg lpos.
Digs i woz xuqor, joo’ji nsulwom o hoxotj yturaqa-fugeg EWO ol UyzfkGqdooq. Yvihezufh o wriqcot ubeukk qunzyiddf amb gejazeviw cazpotv xa gpaf yif be esoy ximq Jsejg Waxvixtudws ev ake ey dce nooh ebo tuxaq ap IxrgfBqvaotb.
Bo, wic rial OhbpvJnyuan ucciapi nhuy? Ffe dezax ot nzik EcfrvDxpois qatgawjw ka EbkcqXeboahji, a kfulixut ifkavatx unrkogicjopy sa suraogu keqeuc aslxhyzuceefhw. Id uhro umribh axkdosetqicd zu jefe oyo ac bgi bac / ojeur / ap nknhiy, du wup soaxp laq daom coz sko nujx cewao ul lwi govauqqu toviqo kowquyfarp ugv janr atariroeb.
Gui big latyos uh IfypjPzyeoz ev sja Wamz on’z vajlarp wuvwil ep wabpupum. Av ztab fuso, fho exMeqkofavaon() sufwbuhf ul xaxtiq yi fvev vwo AqtrjDxpiuv.
Hav nriw vau xcul ewuew OhjmzSgzeob, zodi o ve ej unenh up ar vpi form duspuor.
See forum comments
This content was released on May 20 2025. The official support period is 6-months
from this date.
Understand what AsyncStream is and how to use it appropriately.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
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.