๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IOS๐ŸŽ/WWDC

[WWDC 2021] Meet async/await in Swift

by Jouureee 2022. 5. 17.

What's new in Swift ์˜์ƒ์„ ๋ณด๋‹ค๊ฐ€ async/ await ๋ฌธ๋ฒ•์„ ์ƒˆ๋กญ๊ฒŒ ์†Œ๊ฐœํ•˜๋”๊ตฐ์š” ..!

 

doc๋ฅผ ํ†ตํ•ด ๋จผ์ € ์ •๋ฆฌํ–ˆ๋˜ ์ ์ด ์žˆ๋Š”๋ฐ ์˜์ƒ์„ ๋ณด๋ฉด์„œ ๋ณต์Šตํ•˜๋Š” ๊ฒธ, ๋‹ค์‹œ ํ•œ๋ฒˆ ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๊ฐ„ actor ๋ถ€๋ถ„์„ ๋‹ค์‹œ ๊ณต๋ถ€ํ•ด๋ณด๋ ค๊ตฌ ํ•ฉ๋‹ˆ๋‹ค !!

์•„๋ž˜ ๋น„๋””์˜ค์™€ doc๋ฅผ ๋ณด๋Š” ๊ฒƒ์„ ๊ฐ•๋ ฅํžˆ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค์š” 

 

https://developer.apple.com/videos/play/wwdc2021/10132

 

Meet async/await in Swift - WWDC21 - Videos - Apple Developer

Swift now supports asynchronous functions — a pattern commonly known as async/await. Discover how the new syntax can make your code...

developer.apple.com

https://docs.swift.org/swift-book/LanguageGuide/Concurrency.html

 

Concurrency — The Swift Programming Language (Swift 5.6)

Concurrency Swift has built-in support for writing asynchronous and parallel code in a structured way. Asynchronous code can be suspended and resumed later, although only one piece of the program executes at a time. Suspending and resuming code in your pro

docs.swift.org

 

์šฐ์„  sync์™€ async์˜ ์ฐจ์ด์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

thread๋ฅผ ์ƒ์„ฑํ• ๋•Œ ์ฒซ๋ฒˆ์งธ์˜ ๊ฒฝ์šฐ(sync) ๊ฐ ๋ฉ”์„œ๋“œ ์ˆ˜ํ–‰์ด ๋๋‚ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค์•ผ ํ•ฉ๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ์•„๋ž˜ thread์ฒ˜๋Ÿผ prepareThumnail ๋ฉ”์„œ๋“œ์— completionHandler๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ๋๋‚˜๊ธฐ ์ „๊นŒ์ง€ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๋“ค์„ ์ž์œ ๋กญ๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์ฃ . prepareThumnail๋Š” handler๋กœ ๋๋‚ฌ์Œ์„ ์•Œ๋ฆด ์ˆ˜ ์žˆ๊ณ ์š” 

 

์ด ์™ธ์—๋„ delegate๋ฅผ ์‚ฌ์šฉํ•œ ๋น„๋™๊ธฐ ๋“ฑ ๋‹ค์–‘ํ•œ ๋น„๋™๊ธฐ API๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. completionHandler๋Š” ๋น„๋™๊ธฐ API ์ค‘ ํ•˜๋‚˜์ฃ 

๋ชจ๋“  ๋น„๋™๊ธฐ API์— ๋Œ€ํ•ด ๊ณตํ†ต์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ• ๋•Œ, thread๋ฅผ ๋ธ”๋ฝ์‹œํ‚ค์ง€ ์•Š๊ณ  ๋‹ค๋ฅธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 

 

์˜ˆ์‹œ๋ฅผ ๋ด…์‹œ๋‹ค.

์œ„์˜ ์˜ˆ์‹œ๋Š” ๊ฐ cell์— ๋Œ€ํ•œ image๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค. UIImage๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ๊นŒ์ง€์˜ ๊ณผ์ •์„ ํ•จ๊ป˜ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ฒซ๋ฒˆ์งธ๋กœ url string์œผ๋กœ ๋ถ€ํ„ฐ thumbnailURLRequest๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. 
URLRequest๊ฐ€ ์ƒ์„ฑ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋‘๋ฒˆ์งธ๋กœ URLRequest๋Š” dataTask๋กœ๋ถ€ํ„ฐ data๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  data๋กœ๋ถ€ํ„ฐ UIImage๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋ฐ›์€ image๋กœ๋ถ€ํ„ฐ cell์˜ thumbnail ๋ฅผ renderํ•ฉ๋‹ˆ๋‹ค.

๊ฐ๊ฐ์˜ ์ž‘์—…์€ ์ „ ๋‹จ๊ณ„์— ์˜์กดํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ์ „๋‹จ๊ณ„๊ฐ€ ๋จผ์ € ์ˆ˜ํ–‰๋˜์–ด์•ผ ์ž์‹ ์ด ์ •์ƒ ์ˆ˜ํ–‰ ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด์ง€์š” .

์•„๋ž˜ ๋‘๊ฐœ์˜ ๋ฉ”์„œ๋“œ๋Š” data๋ฅผ ๋น ๋ฅด๊ฒŒ ๋ฐ›์•„ ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํšŒ์ƒ‰ ํ‘œ์‹œ๋œ ๋ฉ”์„œ๋“œ๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  image๋ฅผ ๊ตฌ์„ฑํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. 
์ด๋Ÿฌํ•œ ์ž‘์—…๋“ค์„ ์œ„ํ•ด ๋น„๋™๊ธฐ API๋Š” ํ•„์š”๋กœ ๋ฉ๋‹ˆ๋‹ค. 

์•„๋ž˜ ์ฝ”๋“œ๋Š” ์œ„์˜ flow๋ฅผ ์ž‘์„ฑํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์—๋Š” ๋ฌธ์ œ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
fetchThumnail ํ˜ธ์ถœ์ž๋Š” fetchThumnail ๋ฉ”์„œ๋“œ ์ˆ˜ํ–‰์ด ๋๋‚ฌ์„๋•Œ ๋งŒ์•ฝ ์‹คํŒจํ–ˆ๋”๋ผ๋„ ์•Œ๋ ค์ง€๊ธธ ๋ฐ”๋ž๋‚˜๋””. ํ•˜์ง€๋งŒ ํ˜„์žฌ guard ์•ˆ์— ํ˜ธ์ถœ์ž๋ฅผ ๋– ๋‚˜๋Š” ์ค‘์ž…๋‹ˆ๋‹ค. 

 Guard let else return์„ ํ†ตํ•ด completion์— fail์„ ์ „๋‹ฌํ•ด์•ผ ํ•˜์ง€๋งŒ ๊นŒ๋จน์„ ์ˆ˜๋„ ์žˆ์ฃ . ๊ทธ๋Ÿฌ๋ฉด fetchThumnail์€ ์ด์— ๋Œ€ํ•œ ์•Œ๋ฆผ์„ ๋ฐ›์ง€ ๋ชปํ•˜๊ณ  ๊ณ„์† spin ๋˜๋Š” ui๊ฐ€ ๋‚˜ํƒ€๋‚  ๊ฒƒ์ž…๋‹ˆ๋‹ค. completionHandler์— ๊ตฌํ˜„ํ•˜์ง€ ์•Š์Œ์— ๋”ฐ๋ผ swift error handling ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ด์šฉํ•˜์ง€ ๋ชปํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. 
ํ•ญ์ƒ swift check๊ฐ€ ์ž‘๋™ํ•˜์—ฌ ๋ณด์žฅ๋ฐ›๊ธธ ๋ฐ”๋ผ์ง€๋งŒ swift์—์„œ ์ด๊ฒƒ์„ ๊ฐ•์ œํ•  ๋ฐฉ๋ฒ•์ด ์—†์Šต๋‹ˆ๋‹ค. 


์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด result type์„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „๋ณด๋‹ค ๋” ์•ˆ์ „ํ•ด์กŒ์ง€๋งŒ ๋” ์ฝ”๋“œ๊ฐ€ ๋”๋Ÿฌ์›Œ์กŒ์ฃ  ใ… 

๊ทธ๋ž˜์„œ async/await ๋ฌธ๋ฒ•์ด ํ•ด๊ฒฐํ•ด์ฃผ๊ณ ์ž ํƒ„์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.
Async/await๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ๋ด ๋ด…์‹œ๋‹ค.

ํ•œ๋ˆˆ์— ๋ณด์•„๋„ ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๋” ๊ฐ„๋‹จํ•ด์กŒ์ฃ  ? 

๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ ๋•Œ async ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ค๋‹ˆ๋‹ค. 
๊ทธ๋ฆฌ๊ณ  ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ์— await ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ค๋‹ˆ๋‹ค.

 

await๊ฐ€ ๋ถ™์€ ํ•จ์ˆ˜๋Š” suspend๊ฐ€ ์žˆ์„์ง€ ๋ชจ๋ฅธ๋‹ค๋Š” ๊ฒƒ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋น„๋™๊ธฐ ํ•จ์ˆ˜์—์„œ suspend๋ž€ ๋ฌด์—‡์„ ์˜๋ฏธํ• ๊นŒ์š” ?

fetchThumnail ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ• ๋•Œ thumbnailURLRequest๋ฅผ ํ˜ธ์ถœํ•  ๊ฒƒ์ด๊ณ  ์ด ์ž‘์—…์„ ํ•˜๋Š”๋ฐ thread๋Š” ์™„์ „ํžˆ ์‚ฌ๋กœ์žกํž ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  thumbnailURLRequest๊ฐ€ ๋๋‚˜๊ณ  error๋ฅผ ๋ฐœ์ƒํ•˜๋“  ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋“  ๋‹ค์‹œ fetchThumbnail๋กœ control์„ ๋„˜๊ธธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜๊ฐ€ ์Šค๋ ˆ๋“œ control์„ ํฌ๊ธฐํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ๋น„๋™๊ธฐ์ ์ธ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ํ•จ์ˆ˜๊ฐ€ ๋๋‚  ๊ฒฝ์šฐ control์„ ๋„˜๊ธฐ์ง€๋งŒ ์ผ๋ฐ˜ ํ•จ์ˆ˜์™€ ๋‹ฌ๋ฆฌ ์™„์ „ํžˆ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ์Šค๋ ˆ๋“œ ์ œ์–ด๋ฅผ ํฌ๊ธฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, suspending์ž…๋‹ˆ๋‹ค.

์ผ๋ฐ˜ํ•จ์ˆ˜์™€ ๊ฐ™์ด control์„ ์“ฐ๋ ˆ๋“œ์—๊ฒŒ ๋„˜๊น๋‹ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  suspend๊ฐ€ ๋๋‚ฌ์„๋•Œ ๋‹ค์‹œ ์žฌ๊ฐœํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. 

ํ•˜์ง€๋งŒ await๋ฅผ ์“ด๋‹ค๊ณ  ํ•ด์„œ ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜๋“œ์‹œ suspend ๋œ๋‹ค๋Š” ์˜๋ฏธ๋Š” ์•„๋‹™๋‹ˆ๋‹ค. 


๋˜ ๋‹ค๋ฅธ ์˜ˆ์‹œ๋Š” Doc์—์„œ ๊ฐ€์ ธ์™€๋ดค์Šต๋‹ˆ๋‹ค.

let photoNames = await listPhotos(inGallery: “Summer Vacation”)
let sortedNames = photoNames.sorted()
let name = sortedNames[0]
let photo = await downloadPhoto(named: name)

show(photo)

listPhotos(inGallery:)์™€ downloadPhoto(named:) ํ•จ์ˆ˜ ๋ชจ๋‘ ๋„คํŠธ์›Œํฌ ์š”์ฒญ์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์™„๋ฃŒํ•˜๋Š”๋ฐ ์˜ค๋žœ ์‹œ๊ฐ„์ด ์†Œ์š”๋ฉ๋‹ˆ๋‹ค. async๋ฅผ ์‚ฌ์šฉํ•ด ๋น„๋™๊ธฐ๋กœ ๋งŒ๋“ค๋ฉด ์ด ํ•จ์ˆ˜๊ฐ€ ์ค€๋น„๋ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋™์•ˆ ๋‚˜๋จธ์ง€ ์ฝ”๋“œ๊ฐ€ ๊ณ„์† ์‹คํ–‰๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์•„๋ž˜๋Š” ์œ„์˜ ์˜ˆ์ œ์— ๋Œ€ํ•ด ๊ฐ€๋Šฅํ•œ ์‹คํ–‰ ์ˆœ์„œ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. 
1. ์ฝ”๋“œ๋Š” ์ฒซ๋ฒˆ์งธ ๋ผ์ธ์—์„œ ์‹œ์ž‘๋˜๊ณ  ์ฒซ๋ฒˆ์งธ await ์„ ๋งŒ๋‚ฉ๋‹ˆ๋‹ค.  listPhotos(inGallery:) ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ return ๋˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋™์•ˆ ์‹คํ–‰์„ ์ผ์‹œ์ค‘๋‹จํ•ฉ๋‹ˆ๋‹ค.

2. ์ด ์ฝ”๋“œ์˜ ์‹คํ–‰์ด ์ผ์‹œ์ค‘๋‹จ๋˜๋Š” ๋™์•ˆ, ๊ฐ™์€ ํ”„๋กœ๊ทธ๋žจ ์•ˆ์— ์žˆ๋Š” ๋‹ค๋ฅธ concurrent code ๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ๋“ค์–ด long-running background task๊ฐ€ ํฌํ†  ๊ฐค๋Ÿฌ๋ฆฌ์˜ ๋ชฉ๋ก์„ ๊ณ„์† ์—…๋ฐ์ดํŠธ ํ•˜๊ณ  ์žˆ์„ ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค.
์œ„ task ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‹ค์Œ suspension point (await์ด ํ‘œ์‹œ๋œ)๋ฅผ ๋งŒ๋‚  ๋•Œ ๊นŒ์ง€ ์‹คํ–‰๋  ๊ฒƒ ์ž…๋‹ˆ๋‹ค. 
 
3. listPhotos(inGallery:) ๊ฐ€ return์„ ํ•œ ํ›„์—, ์ฝ”๋“œ๋Š” ์ด ์‹œ์ ๋ถ€ํ„ฐ ์‹คํ–‰์„ ๊ณ„์†ํ•ฉ๋‹ˆ๋‹ค. photoNames ํ”„๋กœํผํ‹ฐ์— ๋ฐ˜ํ™˜๋œ ๊ฐ’์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
 
4. sortedNames๊ณผ name์„ ์ •์˜ํ•˜๋Š” ์ฝ”๋“œ๋Š” ์ผ๋ฐ˜์ ์ธ synchronous code ์ž…๋‹ˆ๋‹ค. ์ด ๋ผ์ธ๋“ค์—๋Š” await ํ‘œ์‹œ๋œ ๊ณณ์ด ์—†์œผ๋ฏ€๋กœ ๊ฐ€๋Šฅํ•œ suspension points ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
 
5. downloadPhoto(named:) function ์„ ์ฝœํ•  ๋•Œ, await ์ด ํ‘œ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ๋Š” ๋‹ค๋ฅธ concurrent code ํ•œํ…Œ ์‹คํ–‰๋  ๊ธฐํšŒ๋ฅผ ์ฃผ๋ฉด์„œ function์ด return ๋  ๋•Œ ๊นŒ์ง€ ์‹คํ–‰์„ ๋‹ค์‹œ ์ค‘๋‹จํ•ฉ๋‹ˆ๋‹ค. 
 
6. downloadPhoto(named:) ๊ฐ€ return ๋œ ํ›„์—, return value๊ฐ€ photo ํ”„๋กœํผํ‹ฐ์— ํ• ๋‹น๋˜๊ณ  photo๋ฅผ argument๋กœ pass ํ•˜๋ฉฐ show(_:) ๋ฅผ ์ฝœํ•ฉ๋‹ˆ๋‹ค. 

์ฝ”๋“œ์—์„œ possible suspension points ๋Š” await ์œผ๋กœ marked ๋˜๊ณ  
์ด๋Š” asynchronous function์ด return๋  ๋•Œ ๊นŒ์ง€ current piece of code๊ฐ€ ์‹คํ–‰์ด ์ค‘์ง€๋  ์ˆ˜ ์žˆ์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
์ด๊ฒƒ์€  yielding the thread (์Šค๋ ˆ๋“œ ์–‘๋ณด)๋ผ ํ• ์ˆ˜ ์žˆ๋Š”๋ฐ ์™œ๋ƒํ•˜๋ฉด ํ™”๋ฉด ๋’ค์—์„œ ํ˜„์žฌ ์Šค๋ ˆ๋“œ์—์„œ ์ฝ”๋“œ์˜ ์‹คํ–‰์„ ์ค‘๋‹จํ•˜๊ณ  ํ˜„์žฌ ์Šค๋ ˆ๋“œ ๋‚ด ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ๋Œ๋ฆด ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. await๊ฐ€ ๋ถ™์€ ์ฝ”๋“œ๋Š” ์ค‘์ง€๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœ๊ทธ๋žจ ๋‚ด ์•„๋ž˜์™€ ๊ฐ™์€ ํ”„๋กœ๊ทธ๋žจ์˜ ํŠน์ • ์œ„์น˜์—์„œ๋งŒ ๋‹ค๋ฅธ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด๋Š” for ๋ฌธ์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

Asynchronous Sequences

์ „ ์„นํ„ฐ์—์„œ listPhotos(inGallery:) ํ•จ์ˆ˜๋Š” ์ „์ฒด ๋ฐฐ์—ด์ด ์ค€๋น„๊ฐ€ ๋˜์—ˆ์„๋•Œ ์ด๋ฅผ ํ•œ๋ฒˆ์— ๋ฆฌํ„ดํ–ˆ์Šต๋‹ˆ๋‹ค. 
๋˜ ๋‹ค๋ฅธ ์ ‘๊ทผ ๋ฐฉ์‹์€ ๋น„๋™๊ธฐ ์‹œํ€€์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ ๋ฒˆ์— ์ปฌ๋ ‰์…˜์˜ ํ•œ ์š”์†Œ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋น„๋™๊ธฐ ์‹œํ€€์Šค์— ๋Œ€ํ•œ ๋ฐ˜๋ณต์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

import Foundation

let handle = FileHandle.standardInput
for try await line in handle.bytes.lines {
    print(line)
}

 

์ผ๋ฐ˜์ ์ธ for-in ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹ ์— ์˜ˆ์ œ์—์„œ๋Š” for๊ณผ await๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ• ๋•Œ์™€ ๋น„์Šทํ•˜๊ฒŒ await๋ฅผ ์“ฐ๋Š”๊ฒƒ์€ ์ค‘์ง€์ ์ด ๋  ์ˆ˜ ์žˆ์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. for -await -in ๋ฃจํ”„๋Š” ์ž ์žฌ์ ์œผ๋กœ ๊ฐ ๋ฐ˜๋ณต์˜ ์‹œ์ž‘์—์„œ ๋‹ค์Œ ์š”์†Œ๊ฐ€ ์ด์šฉ๊ฐ€๋Šฅํ•ด์งˆ๋•Œ๊นŒ์ง€ ์‹คํ–‰์„ ์ค‘์ง€ํ•ฉ๋‹ˆ๋‹ค. For-in ๋ฃจํ”„๊ฐ€ Sequence ํ”„๋กœํ† ์ฝœ์„ ์ˆœ์‘ํ•ด์•ผ ํ–ˆ๋˜๊ฒƒ ์ฒ˜๋Ÿผ for -await -in ๋ฃจํ”„๋Š” AsyncSequence๋ฅผ ์ˆœ์‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 


await๋กœ suspend๋˜์—ˆ์„๋•Œ ์•„๋ž˜ ์ฝ”๋“œ๋Š” transaction์œผ๋กœ ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ์—์„œ ์ˆ˜ํ–‰๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•œ ์ด์Šˆ๋Š” https://developer.apple.com/videos/play/wwdc2021/10133๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”

 

Protect mutable state with Swift actors - WWDC21 - Videos - Apple Developer

Data races occur when two separate threads concurrently access the same mutable state. They are trivial to construct, but are notoriously...

developer.apple.com

Summary
1. async ํ•จ์ˆ˜๋ฅผ suspend ํ•ฉ๋‹ˆ๋‹ค.
2. await async ํ•จ์ˆ˜๊ฐ€ suspend ๋ ์ˆ˜ ์žˆ์Œ์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
3. suspend๋˜๋Š” ๋™์•ˆ ํ•ด๋‹น thread๋Š” ๋‹ค๋ฅธ ์ž‘์—…์„ ์ž์œ ๋กญ๊ฒŒ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
4. async ํ•จ์ˆ˜๊ฐ€ resume๋˜๋ฉด async ํ•จ์ˆ˜๋กœ๋ถ€ํ„ฐ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ๊ณณ์—์„œ async/await๋ฅผ ์‚ฌ์šฉ ํ•˜๋Š” ์˜ˆ์‹œ๋ฅผ ๋ด๋ด…์‹œ๋‹ค.
Testing code์ž…๋‹ˆ๋‹ค. 

thumnailView๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ์žˆ๋Š” image ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ๊ฐ ์•„์ด๋””๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€๋ฅผ ๋น„๋™๊ธฐ์ ์œผ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๋ถ€๋ถ„์„ ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ณ€ํ™˜์‹œ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ complier ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ์š” onApear ํ•จ์ˆ˜๋Š” sync ํด๋กœ์ €๋กœ async ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. 

์ด์— ๋Œ€ํ•œ ํ•ด๊ฒฐ์ฑ…์œผ๋กœ async ํ•จ์ˆ˜์ธ Task๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 
async Task๋Š” ํด๋กœ์ €์˜ ์ž‘์—…์„ ํŒจํ‚ค์ง€ํ™”ํ•˜๊ณ  global DispatchQueue์˜ ๋น„๋™๊ธฐ ํ•จ์ˆ˜์™€ ๊ฐ™์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ next ์Šค๋ ˆ๋“œ์—์„œ ์ฆ‰์‹œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ์‹œ์Šคํ…œ์œผ๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค.
์ด๊ฒƒ์˜ ์ฃผ์š”ํ•œ ์ด์ ์€ async ์ฝ”๋“œ๊ฐ€ sync context์•ˆ์—์„œ ํ˜ธ์ถœ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.

๋˜๋Š” core data์—์„œ data๋ฅผ handlingํ•˜๋Š” ์˜ˆ์‹œ์—์„œ๋„ ์—ญ์‹œ

 

Core data์—์„œ completion Handler๋กœ ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ getPersistentPosts์— ๋ฆฌํ„ดํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ continuation์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

๋‹ค์‹œ ์ฝ”๋“œ๋กœ ๋ด ๋ด…์‹œ๋‹ค. withCheckedThrowingContinuation ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด continuation์œผ๋กœ ๋ฐ›์•„์˜จ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

continuation์€ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ด์ง€๋งŒ ์ฃผ์˜์‚ฌํ•ญ(contract)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 
resum์€ ๋ชจ๋“  path์— ํ•œ๋ฒˆ ๊ผญ ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•œ path์— ์—ฌ๋Ÿฌ๋ฒˆ ํ˜ธ์ถœ๋  ๊ฒฝ์šฐ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๋ฅผ ํ˜ธ์ถœํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. 

 

 

Task์™€ Action์€ ๋‹ค์Œ ์„ธ์…˜์„ ๋ณด๊ณ  ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค .. Concurrency ์„ธ์…˜์ด ์ •๋ง ๋งŽ๋”๊ตฐ์š” ๋‹ค ํ•˜๋‚˜ ํ•˜๋‚˜ ์ฝ์–ด ๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค ..!

๋Œ“๊ธ€