Jiseob Kim

iOS Developer

Swift - Notification Center

27 Oct 2018 » Swift

**์ฃผ์˜ : ํ˜น์‹œ ์ฝ์ง€๋งŒ ๋๊นŒ์ง€ ์•ˆ๋ณด์‹œ๋Š” ๋ถ„๋“ค์€ ์ ์šฉํ•˜๊ณ  `์˜ต์ €๋ฒ„ ์ œ๊ฑฐ` ๊ฒ€์ƒ‰ํ•ด์„œ ๊ผญ ์ œ๊ฑฐํ•ด์ฃผ์„ธ์š”.**

์˜ค๋Š˜ ํฌ์ŠคํŒ… ์ฃผ์ œ๋Š” Notification Center.

Swift๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์‚ฌ์šฉํ•œ์ ์ด ์—†์—ˆ๋˜๊ฒƒ ๊ฐ™์•„์„œ ๋ฐฐ์šฐ๋Š”๊น€์— ๊ธ€๋กœ ํฌ์ŠคํŒ…์œผ๋กœ!!!

ํ•˜์ง€๋งŒ, ์•„์ง๋„ ์ •๋ง ์ด๋•Œ ์จ์•ผํ•ด!! ๋ผ๋Š”๊ฑด ์ž˜๋ชจ๋ฅด๊ฒ ๋‹คโ€ฆ ์ด๋Ÿฐ ๊ฒฝ์šฐ์— ์ด๊ฑธ ์“ธ์ˆ˜์žˆ๊ฒ ๋‹ค ์ •๋„ ๋Š๋‚Œโ€ฆ?

๋งˆ์น˜ if๋ฌธ ๋ฐฐ์šด์ƒํƒœ์„œ guard๋ฌธ์„ ์•Œ๊ฒŒ๋œ ๋Š๋‚Œโ€ฆ

๊ฐ„๋‹จํ•œ ๊ฐœ๋…์ ๋ถ€ํ„ฐ ์ ‘๊ทผ์„ ํ•˜์ž๋ฉด



๋ฐฐ์šฐ๊ณ ๋‚œ ๋Š๋‚Œ


๋Š๋‚Œ์€ ์ด๋žฌ๋‹ค

ย <๋ฌด์–ธ๊ฐ€ ์•ก์…˜์ด ์žˆ์—ˆ์„ ๋•Œ,>์ด ์•ก์…˜์ด ์ง„ํ–‰๋˜์—ˆ์œผ๋‹ˆ ๋‹ด๋‹น์ž๋“ค์€ ์ฒ˜๋ฆฌ๋ฐ”๋žŒ.

ย ๋‹ด๋‹น์ž ์ˆ˜์‹  ์™„๋ฃŒ, ์ฒ˜๋ฆฌํ•˜๊ฒ ์Œย 


์ด๋Ÿฐ ๋Š๋‚Œ??

์•ฑ์—์„œ ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ€๋Šฅํ• ๊ฒƒ ๊ฐ™์•˜๋‹ค


ํ™”๋ฉด์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ์— ๋“ค์–ด๊ฐ”์„๋•Œ, VC์— ์–ด๋– ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ณ  ์‹ถ๋‹ค.

์—ฌ๋Ÿฌ VC์— ์˜ํ–ฅ์„ ์ฃผ๊ณ  ์‹ถ๋‹ค.


๋ณดํ†ต์˜ ๊ฒฝ์šฐ ๊ฐ™์€ ํด๋ž˜์Šค ๋‚ด์˜ ๋ฉ”์†Œ๋“œ๋‚˜ ํ”„๋กœํผํ‹ฐ๋“ค์ด ์•„๋‹ˆ๋ฉด ์ ‘๊ทผํ•˜๊ธฐ๊ฐ€ ๊นŒํƒˆ์Šค๋Ÿฌ์› ๋˜๊ฒƒ ๊ฐ™๋‹ค.

๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ํŒจํ„ด์„ ์“ด๋‹ค๊ฑฐ๋‚˜ ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•ด์„œ ์ ‘๊ทผ์„ ํ•˜๊ฒŒ๋˜์ง€๋งŒ Notification Center๋กœ๋„ ๊ฐ€๋Šฅํ•˜๋‹จ๊ฑธ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ, ๋‹จ์ˆœ ๊ฐ’์ „๋‹ฌ์ด๋ผ๋˜๊ฐ€, ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ํŒจํ„ด, ํ”„๋กœํ† ์ฝœ์ด ์‚ฌ์šฉํ•˜๊ธฐ ๋‚œ๊ฐํ•œ ์ƒํ™ฉ์ด ์•„๋‹Œ์ด์ƒ Noti Center๋Š” ์•ˆ์“ธ๋“ฏํ•˜๋‹ค.

์ข€๋” ์—ฌ๋Ÿฌ๊ณณ์— ๋™์‹œ๋‹ค๋ฐœ์ ์œผ๋กœ ์•Œ๋ฆฌ๋Š”๊ฒŒ ๊ฐ€๋Šฅํ•˜๊ฒ ๋‹ค๋ž€ ์ƒ๊ฐ์ด ๋“ค๋ฉด์„œ

์ฑ…๋“ค์ด ์™œ ๋ฐฉ์†ก๊ตญ์— ์˜ˆ์‹œ๋ฅผ ๋“œ๋Š”์ง€ ์กฐ๊ธˆ ์ดํ•ด๊ฐ€ ๋๋‹ค.



์‚ฌ์šฉ๋ฒ•


๋ฐฉ์†ก๊ตญ์ด๋‹ค ๋ญ๋‹คํ•ด๋„ ์ฒจ์—” ๋‚œ ์ดํ•ด๊ฐ€ ์•ˆ๋์—ˆ๊ธฐ์— ๊ทธ๋Ÿฐ ์„ค๋ช…์€ ์ƒ๋žต

<๋ฌด์–ธ๊ฐ€ ์•ก์…˜์ด ์žˆ์—ˆ์„ ๋•Œ,>์ด ์•ก์…˜์ด ์ง„ํ–‰๋˜์—ˆ์œผ๋‹ˆ ๋‹ด๋‹น์ž๋“ค์€ ์ฒ˜๋ฆฌ๋ฐ”๋žŒ.

๋‹ด๋‹น์ž ์ˆ˜์‹  ์™„๋ฃŒ, ์ฒ˜๋ฆฌํ•˜๊ฒ ์Œ

์ด๋ ‡๊ฒŒ ์“ด ์ด์œ ๊ฐ€ ์žˆ๋‹ค. ์ฝ”๋“œ ๋™์ž‘์„ ํ’€์–ด์“ด๊ฒƒ์ด์—ˆ๋‹ค.



Post - โ€œ์ด ์•ก์…˜์ด ์ง„ํ–‰๋˜์—ˆ์œผ๋‹ˆ ๋‹ด๋‹น์ž๋“ค์€ ์ฒ˜๋ฆฌ๋ฐ”๋žŒโ€

์šฐ์„  <๋ฌด์–ธ๊ฐ€ ์•ก์…˜์ด ์žˆ์—ˆ์„ ๋•Œ,>์ด ์•ก์…˜์ด ์ง„ํ–‰๋˜์—ˆ์œผ๋‹ˆ ๋‹ด๋‹น์ž๋“ค์€ ์ฒ˜๋ฆฌ๋ฐ”๋žŒ.๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

// doItSomeThing์— ํ•ด๋‹น ๋˜๋Š” ๊ฒƒ๋“ค์€ ์ฒ˜๋ฆฌ ๋ฐ”๋žŒ
NotificationCenter.default.post(name: .doItSomeThing, object: nil)

์ค‘์š” - post

๋‚˜์ค‘ ์„ค๋ช… - .doItSomeTing, object

์ฐธ๊ณ ๋กœ .doItSomeThing์€ ์›๋ž˜ ์กด์žฌํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๊ณ  ๋‚ด๊ฐ€ ์ƒ์„ฑํ•œ ํ”„๋กœํผํ‹ฐ,

์•„๋ž˜์—์„œ ์„ค๋ช…!

ํฌ์ŠคํŠธ๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์ค‘์š”ํ•˜๋‹ค.๋ญ”๊ฐ€ ๋ณด๋‚ด๋Š” ๊ทธ๋Ÿฐ ์˜๋ฏธ๋‹ˆ, ์—ฌ๊ธฐ์„  name์— ํ•ด๋‹น์ž๋“ค์—๊ฒŒ ๊ฐ์ž ์ผ์„ ์ˆ˜ํ–‰ํ•˜๋ผ๊ณ  ์‹คํ–‰์‹œํ‚ค๋Š” ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•˜๋Š”๊ฒŒ ๋งž๋Š”๊ฒƒ ๊ฐ™๋‹ค.

๊ทธ๋Ÿผ ์‹คํ–‰ํ•˜๋ผ ๋ช…๋ นํ–ˆ์œผ๋‹ˆ ๋ช…๋ น ๋ฐ›์„ ์• ๋“ค์ด ํ•„์š”ํ•˜๋‹ค.

๋‹ค์†Œ ํŠน์ดํ•œ ๊ฐœ๋…์ด ๋‚˜์˜จ๋‹ค. doItSomeThing์€ ์ •๋ง name์— ํ•ด๋‹น๋œ๋‹ค. ๋ฌด์Šจ ๋™์ž‘ํ•˜๋ž€๊ฒŒ ์•„๋‹ˆ๋‹ค.

name์„ ์ผ ์ฒ˜๋ฆฌํ•˜๋Š” ์Šคํ…์˜ ์ด๋ฆ„์ด๋ผ ์ƒ๊ฐํ•ด์•ผํ•œ๋‹ค.

์Šคํ…๋งˆ๋‹ค ๊ฐ์ž์˜ ์ผ๊ฑฐ๋ฆฌ๋Š” ๋‹ค๋ฅด๋‹ค.

๊ทธ๋ ‡๋‹ด, ์ผ๊ฑฐ๋ฆฌ์™€ ์ด๋ฆ„์„ ์…‹ํŒ…์„ ํ•˜์ž.

์ผ๋‹จ ์ผ๊ฑฐ๋ฆฌ ํ•˜๋‚˜ ๋งŒ๋“ ๋‹ค. ( = ๋™์ž‘ํ•  ํ•จ์ˆ˜ ์ƒ์„ฑ )

@objc func printSomeThing(_ notification: Notification) {
        print("do it something")
    }

๊ฐ„๋‹จํ•˜๋‹ค. ํ•˜๋Š”์ผ์€ ๊ทธ์ € print ํ•œ๋ฒˆ ํ•ด์ฃผ๋Š”๊ฒƒ๋ฟ.

๋ˆˆ์—ฌ๊ฒจ๋ณผ๊ฑฐ๋ผ๋ฉด ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…์ด Notification์ด๋ผ๋Š” ์ •๋„???

@objc ๋ถ™์€ ์ด์œ ๋Š” NotificationCenter๊ฐ€ ์›๋ž˜๋Š” NSNotificationCenter๋‹ค. Swift ๋ช‡์ธ์ง€ ๊ธฐ์–ต์€ ์•ˆ๋‚˜์ง€๋งŒ ์ด๋ฆ„์ด ๋ฐ”๋€Œ์—ˆ๋‹ค. Objective-c ์—์„œ ์“ฐ์ด๋Š”๊ฑฐ๋‹ˆ ๋ถ™์—ฌ์ฃผ์ž



Observer - โ€œ๋‹ด๋‹น์ž ์ˆ˜์‹  ์™„๋ฃŒ, ์ฒ˜๋ฆฌํ•˜๊ฒ ์Œโ€

observer = ๊ด€์ฐฐ์ž, ์—ฌ๊ธฐ์„  ์ผํ•˜๋Š” ์Šคํ…(๋‹ด๋‹น์ž)์ด๋ผ ์ƒ๊ฐํ•˜์ž, ์Šคํ…๋“ค ์—…๋ฌด ์ง€์ •์ •๋„?

// ๋‹ด๋‹น์ž ์ธ์ ์‚ฌํ•ญ ๋ฐ ์—…๋ฌด ์ถ”๊ฐ€
NotificationCenter.default.addObserver(self, selector: #selector(printSomeThing(_:)), name: .doItSomeThing, object: nil)

์ค‘์š” - addObserver, selector , name

addObserver - ๊ด€์ฐฐ์ž๋ฅผ ๋Œ€๊ธฐ์‹œํ‚ค๊ฒ ๋‹ค๋Š” ๊ฒƒ,

selector - ์Šคํ…Œํ”„ ์—…๋ฌด

name - ์Šคํ…Œํ”„ ์ด๋ฆ„


์—ฌ๊ธฐ์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ๊ฑด ์ด๋ฆ„์ด๋ผ ์ƒ๊ฐํ•œ๋‹ค. Post ๋ฉ”์†Œ๋“œ๊ฐ€ name์— โ€œ๋ˆ„๊ตฌ์”จ ์ผํ•ด!!โ€ ๋ผ๊ณ  ํ•œ๋‹ค๋ฉด, ๊ทธ ์ด๋ฆ„์— ํ•ด๋‹น ๋˜๋Š” ์Šคํ…๋“ค์ด ๊ฐ์ž ๋“ฑ๋ก๋œ ์—…๋ฌด (= selector) ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค. ์ด ์ฝ”๋“œ์—์„  .doItSomeTing์„ ๋ถ€๋ฅด๋ฉด ์•„๊นŒ ๋งŒ๋“  ํ•จ์ˆ˜(print ํ•œ์ค„์งœ๋ฆฌ)๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.

์Šคํ… ์ด๋ฆ„์€ ์ค‘๋ณต๊ฐ€๋Šฅํ•˜๋‹ค, ๋‹ค๋ฅธ addObserver๋ฅผ ์ž‘์„ฑํ•ด๋„ ์ด๋ฏธ ์‚ฌ์šฉํ•œ ์ด๋ฆ„์„ ์“ธ์ˆ˜์žˆ๋‹ค.

์ด๋Ÿด๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰๋งŒ์ด ์•„๋‹Œ ํ•ด๋‹น ์ด๋ฆ„ ๋ชจ๋‘ ์‹คํ–‰๋จ.



name ๋“ฑ๋ก ๋ฐฉ์‹

๊ทธ๋Ÿผ ์Šฌ์Šฌ ์ด๋ฆ„ ์ถ”๊ฐ€๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์“ฐ๋Š”๊ฒŒ ๋งž๋Š”๊ฒƒ ๊ฐ™๋‹ค.

2๊ฐ€์ง€ ์ •๋„๊ฐ€ ์žˆ๋‹ค.

1) addObserverํ• ๋•Œ ํ•œ๋ฒˆ์—

// Post
NotificationCenter.default.post(name: Notification.Name("doItSomeThing"), object: nil)
// Add Observer
NotificationCenter.default.addObserver(self, selector: #selector(printSomeThing(_:)), name: Notification.Name("doItSomeThing"), object: nil)

์ด๋ ‡๊ฒŒ โ€œdoItSomeThingโ€ ์ด๋ผ๋Š” Notification.Name ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋งŒ๋“ค์ˆ˜๋„ ์žˆ์ง€๋งŒ

๋ณ„๋กœ ์ถ”์ฒœํ•˜๊ณ  ์‹ถ์ง€ ์•Š๋‹ค. ์žฌ์‚ฌ์šฉ๋„ ํž˜๋“ค๊ณ  ์ฝ”๋“œ๋„ ์•ˆ๊น”๋”


2) Extension์œผ๋กœ ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€

extension Notification.Name {
    static let doItSomeThing = Notification.Name("doItSomeThing")
}

์ด๋ ‡๊ฒŒ Notification.Name์— ํ”„๋กœํผํ‹ฐ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด,

์œ„ ์ฝ”๋“œ๋“ค์— ๋‚˜์˜ค๋“ฏ name์ž๋ฆฌ์— ์žˆ๋˜ .doItSomeThing์„ ์“ธ์ˆ˜ ์žˆ๊ธฐ์—

๊น”๋”ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ๋„ ์ˆ˜์›”ํ•ด์ง„๋‹ค.



์˜ˆ์‹œ


์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค

ex) ํ™”๋ฉด์ด VC1 -> VC2 -> VC3 ์ด๋ ‡๊ฒŒ ์žˆ๋‹ค๊ณ  ์น˜์ž, ๊ทธ๋ฆฌ๊ณ  ๊ฐ๊ฐ Label์„ 1๊ฐœ์”ฉ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

๋„ค๋น„๊ฒŒ์ด์…˜์„ ์ด์šฉํ• ๊ฒƒ์ด๋ฉฐ, ํ™”๋ฉด ์ด๋™๋„ ์œ„์™€ ๊ฐ™๋‹ค. ๊ทธ๋ฆผ์œผ๋กœ ์ดํ•ด๋•์ž๋ฉด ์ด์ •๋„?

๊ทธ๋ฆฌ๊ณ  ๊ทธ 3์—๋Š” ๋ฒ„ํŠผ์ด ํ•˜๋‚˜ ์žˆ๊ณ , ์ด๊ฒƒ์„ ๋ˆ„๋ฅด๋ฉด Post๋ฅผ ํ•œ๋‹ค.


์šฐ์„  ์ด๋ฆ„๋ถ€ํ„ฐ ์ถ”๊ฐ€

extension Notification.Name {
    static let changeLabel = Notification.Name("changeLabel")
}


๊ทธ๋ฆฌ๊ณ ,

ViewController 3๊ฐœ๊ฐ€ ์žˆ๊ณ 

๊ฐ ViewDidLoad์— addObserverํ•ด์ค€๋‹ค.

/////////////////VC1
// ViewDidLoad
NotificationCenter.default.addObserver(self, selector: #selector(change1), name: .changeLabel, object: nil)

// change1 ํ•จ์ˆ˜ ๋‚ด์šฉ
self.label1.text = "Change Label1!!!"

/////////////////VC2
// ViewDidLoad
NotificationCenter.default.addObserver(self, selector: #selector(change2), name: .changeLabel, object: nil)

// change2 ํ•จ์ˆ˜ ๋‚ด์šฉ
self.label2.text = "Change Label2!!!"

/////////////////VC3
// ViewDidLoad
NotificationCenter.default.addObserver(self, selector: #selector(change3), name: .changeLabel, object: nil)

// change3 ํ•จ์ˆ˜ ๋‚ด์šฉ
self.label3.text = "Change Label3!!!"


๋งˆ์ง€๋ง‰์œผ๋กœ Bnt ๋ฒ„ํŠผ ๋ฉ”์†Œ๋“œ ์ •์˜

NotificationCenter.default.post(name: .changeLabel, object: nil)



๊ฒฐ๊ณผ


๊ทธ๋Ÿผ gif๋Š” ํ• ์ค„ ๋ชจ๋ฅด๋‹ˆ (๋ฐฉ๊ธˆ ๋ฐฐ์›€. ๋‹จ์ถ•์–ด ์งฑ์งฑ)

์œ„ Gif์— ๋‚˜์˜จ๊ฒƒ์ฒ˜๋Ÿผ, ์ง์ „ VC2๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ˜„์žฌ ์Šคํƒ์— ์žˆ๋Š” VC1๊นŒ์ง€ ๋ชจ๋‘ ์ž‘๋™ํ–ˆ๋‹ค.



์˜ต์ €๋ฒ„ ์ œ๊ฑฐ


๋‹ค๋ฅธ ์˜๋ฏธ๋กœ ์—ฌ๊ธฐ์„œ ์ž˜ ์ƒ๊ฐํ•ด๋ด์•ผํ•˜๋Š”๊ฒŒ, observer๊ฐ€ ๊ณ„์† ์‚ด์•„์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

๊ทธ๋ ‡๋‹ด ์ œ๊ฑฐ๋Š” ๋”ฐ๋กœ ํ•ด์ค˜์•ผํ•œ๋‹ค๋Š”๊ฒƒ์ด๋‹ค. ๋ถˆํ•„์š”ํ•œ๋ฐ ๊ณ„์† ๋‚จ์•„์žˆ์–ด์„œ ์ข‹์„๊ฒƒ์€ 1๋„ ์—†์œผ๋‹ˆ๊น!

์ œ๊ฑฐ๋Š” ๊ฐ„๋‹จ.

NotificationCenter.default.removeObserver(self, name: .changeLabel, object: nil)



์ถ”๊ฐ€ : Postํ• ๋•Œ ๊ฐ’๋„ ์ „๋‹ฌ


Post๋ฉ”์†Œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ์— object๋„ ์žˆ์—ˆ๋‹ค. ์œ„์—์„  nil์„ ๋„ฃ์—ˆ์ง€๋งŒ, ์ด ๋ถ€๋ถ„์„ ํ†ตํ•ด ๊ฐ’์„ ๋ณด๋‚ผ์ˆ˜ ์žˆ๋‹ค.

๊ธฐ์กด selector์— ๋“ค์–ด๊ฐ€๋˜ ํ•จ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•ด์ฃผ์ž(VC2๋ž‘ VC3๋„ ๋ณ€๊ฒฝ!)

// ๊ธฐ์กด
@objc func change1(_ notification: Notification) {
        self.label1.text = "Change Label1!!!"
    }

// ๋ณ€๊ฒฝ
@objc func change1(_ notification: Notification) {
    let getValue = notification.object as! String
    self.label1.text = "Change \(getValue)"
}

์—ฌ๊ธฐ์„  String์œผ๋กœ๋งŒ ๋ณด๋‚ผ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ•์ œ ๋‹ค์šด ์บ์ŠคํŒ…! (as!)


btn์˜ Post๋„ ์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝ

ํƒ€์ž…์ด Notification์ธ ํŒŒ๋ผ๋ฏธํ„ฐ notification์— object๊ฐ€ Post์— ์žˆ๋˜ object๋‹ค.

// ๊ธฐ์กด
@IBAction func doPost(_ sender: UIButton) {
        NotificationCenter.default.post(name: .changeLabel, object: nil)
    }

// ๋ณ€๊ฒฝ
@IBAction func doPost(_ sender: UIButton) {
        NotificationCenter.default.post(name: .changeLabel, object: "Object")
    }


๊ฒฐ๊ณผ



PS. ๋” ์ ์ ˆํ•œ ์˜ˆ์‹œ๋ฅผ ์•„์‹ ๋‹ค๋ฉด ๋Œ“๊ธ€ ๋ถ€ํƒ ๋“œ๋ฆฝ๋‹ˆ๋‹ค!! ๊ฐ™์ด ๊ณต๋ถ€ํ•ด์š”!