๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS

[iOS/Swift] ๋ช‡ ์ดˆ ํ›„์— ์ฝ”๋“œ ์‹คํ–‰ํ•˜๊ธฐ DispatchQueue asyncAfter

by ํ‹ด๋”” 2022. 3. 19.
๋ฐ˜์‘ํ˜•

DispatchQueue ํด๋ž˜์Šค์— ์žˆ๋Š” 

    public func asyncAfter(deadline: DispatchTime, execute: DispatchWorkItem)

์„ ์‚ฌ์šฉํ•ด ์ค๋‹ˆ๋‹ค

deadline์€ DispatchTime์„ ๋ฐ›๋Š”๋ฐ .now() ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ˜„์žฌ ์‹œ๊ฐ„์„ ๋ฐ˜ํ™˜ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. execute์—๋Š” deadline ์ดํ›„์— ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ๋ฅผ ํด๋กœ์ €๋กœ ์ „๋‹ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค

3์ดˆ ํ›„์— execute์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ ์ž ํ•  ๋•Œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹น

        DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: {
            // ์‹คํ–‰ํ•  ์ฝ”๋“œ ์ž‘์„ฑ
        })

์˜ˆ์‹œ

        DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: {
            self.dismiss(animated: false, completion: nil)
            DispatchQueue.main.async {
                self.completion()
            }
        })

์ด ์ฝ”๋“œ๋Š” 3์ดˆ ํ›„์— ํ˜„์žฌ ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋‹ซ๊ณ  completion ์„ ์‹คํ–‰ํ•˜๋Š” ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค

completion ํด๋กœ์ €๋Š” UI๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์–ด main ์Šค๋ ˆ๋“œ์—์„œ ์ˆ˜ํ–‰๋˜๋„๋ก ์ž‘์„ฑํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€