Skip to content

Latest commit

ย 

History

History
99 lines (70 loc) ยท 3.31 KB

Escaping-Closure.md

File metadata and controls

99 lines (70 loc) ยท 3.31 KB

Escaping Closure(ํƒˆ์ถœ ํด๋กœ์ €)์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜์‹œ์˜ค

์ฐธ๊ณ ํ•œ ์ข‹์€ ๊ธ€

Answer

Escaping Closure(@escaping) : ํด๋กœ์ €๊ฐ€ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ „๋‹ฌ ๋์„ ๋•Œ, ํ•จ์ˆ˜์˜ ์‹คํ–‰์ด ์ข…๋ฃŒ๋œ ํ›„ ์‹คํ–‰๋˜๋Š” ํด๋กœ์ €.

Non-Escaping Closure

func runClosure(closure: () -> Void) {
    closure()
}
  1. ํด๋กœ์ €๊ฐ€ runClosure()ํ•จ์ˆ˜์˜ closure ์ธ์ž๋กœ ์ „๋‹ฌ๋จ
  2. ํ•จ์ˆ˜ ์•ˆ์—์„œ closure()๊ฐ€ ์‹คํ–‰๋จ
  3. runClosure() ํ•จ์ˆ˜๊ฐ€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ข…๋ฃŒ๋จ

Escaping Closure(@escaping)

class ViewModel {
    var completionhandler: (() -> Void)? = nil

    func fetchData(completion: @escaping () -> Void) {
        completionhandler = completion
    }
}
  1. ํด๋กœ์ €๊ฐ€ fetchData() ํ•จ์ˆ˜์˜ completion ์ธ์ž๋กœ ์ „๋‹ฌ๋จ
  2. ํด๋กœ์ € completion์ด completionhandler ๋ณ€์ˆ˜์— ์ €์žฅ๋จ
  3. fetchData() ํ•จ์ˆ˜๊ฐ€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ข…๋ฃŒ๋จ
  4. ํด๋กœ์ € completion์€ ์•„์ง ์‹คํ–‰๋˜์ง€ ์•Š์Œ

๋ณด๋‹ค ๋ช…์‹œ์ ์ธ ์˜ˆ์‹œ

class Myclass {
    var x = 10
    
    func callFunc() {
        withEscaping { self.x = 100 }
        withoutEscaping { x = 200 } 
    }

    var completionHandlers: [() -> Void] = []

    func withEscaping(completion: @escaping () -> Void) {
        completionHandler.append(completion)
    }

    func withoutEscaping(completion: () -> Void) {
        completion()
    }
}

&

let mc = MyClass()

mc.callFunc() // 1.
print(mc.x) 	// 2. result:200

mc.completionHandlers.first?()
print(mc.x)	// 3. result:100
  1. ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ callFunc() ํ˜ธ์ถœ
  2. @escaping๋กœ ๋ช…์‹œํ•ด์ฃผ์ง€ ์•Š์•˜๋˜ withoutEscaping ๋ฉ”์†Œ๋“œ๊ฐ€ ํด๋กœ์ €๋ฅผ ๋ฐ”๋กœ ํ˜ธ์ถœํ•˜๋ฏ€๋กœ 200 ์ถœ๋ ฅ
    • ์ด๋•Œ @escaping์œผ๋กœ ๋ช…์‹œํ•ด๋‘” ๋ฉ”์„œ๋“œ์—์„œ๋Š” ํด๋กœ์ €๋ฅผ completionHandler์— appendํ•˜์—ฌ ์ €์žฅ
  3. completionHandler ๋ฐฐ์—ด์— ์ €์žฅํ•ด๋‘” ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š” ํด๋กœ์ €๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ทธ์ œ์„œ์•ผ 100 ์ถœ๋ ฅ

์ด์ฒ˜๋Ÿผ ํƒˆ์ถœ ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํด๋กœ์ €๋ฅผ ํ•จ์ˆ˜ ๊ตฌ๋ฌธ ๋ฐ–์—์„œ๋„ ํ˜ธ์ถœ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.



๐Ÿ“Œ ํƒˆ์ถœ ํด๋กœ์ €๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ผญ @escaping์œผ๋กœ ๋ช…์‹œํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋ช…์‹œํ•ด๋‘๊ณ  Non-Escaping ํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ•ด๋„ ์ƒ๊ด€ ์—†๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ๊ทธ๋ƒฅ @escaping ์„ ์–ธ์„ ํ•ญ์ƒ default๋กœ ํ•ด๋‘๋ฉด ๋‘˜ ๋‹ค ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋” ์ข‹์ง€ ์•Š์„๊นŒ?

๊ตณ์ด Non-Escaping, Escaping์œผ๋กœ ๋‚˜๋ˆ„๋Š” ์ด์œ 

๋ฐ”๋กœ ์ปดํŒŒ์ผ๋Ÿฌ์˜ ํผํฌ๋จผ์Šค์™€ ์ตœ์ ํ™” ๋•Œ๋ฌธ์ด๋‹ค.

Non-Escaping ํด๋กœ์ €์˜ ๊ฒฝ์šฐ, ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํด๋กœ์ €์˜ ์‹คํ–‰ ์ข…๋ฃŒ ์‹œ์ ์ด ์–ธ์ œ์ธ์ง€ ์•Œ๊ธฐ ๋•Œ๋ฌธ์— ๋•Œ์— ๋”ฐ๋ผ ํŠน์ • ๊ฐ์ฒด์— ๋Œ€ํ•œ retain, release์˜ ์ฒ˜๋ฆฌ๋ฅผ ์ƒ๋žตํ•˜์—ฌ life-cycle์„ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ Escaping ํด๋กœ์ €๋Š” ํ•จ์ˆ˜์˜ ๋ฐ–์—์„œ ์‹คํ–‰ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ํด๋กœ์ €์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ถ”๊ฐ€์ ์ธ ์ฐธ์กฐ์‹ธ์ดํด(reference cycles) ๊ด€๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์ด๋Ÿฌํ•œ ์ด์œ ๋กœ Swift์—์„œ๋Š” ํ•„์š”์‹œ์—๋งŒ Escapingํด๋กœ์ €๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๊ตฌ๋ถ„๋˜์–ด ์žˆ๋‹ค!