Notification Center๋Š” ๊ฝค๋‚˜ ์˜ค๋ž˜์ „ ๋ถ€ํ„ฐ iOS์˜ ์ด๋ฒคํŠธ ์ „์†ก ๋งค์ปค๋‹ˆ์ฆ˜์œผ๋กœ ์ž๋ฆฌ์žก์•˜๋‹ค. ์–ด๋–ค ์š”์†Œ๋“ค์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

Publisher/Subscriber Pattern

NotificationCenter์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ธฐ ์ด์ „์—, ์ด ๋…€์„์ด ์–ด๋– ํ•œ ๊ธฐ๋ฐ˜์„ ๊ฐ€์ง€๊ณ  ํƒœ์–ด๋‚œ ์•„์ด์ธ์ง€ ์•Œ์•„๋ณผ ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ NotificationCenter๋Š” Publisher/Subscriber Pattern์„ ๋”ฐ๋ฅธ๋‹ค. Publisher/Subscriber Pattern์€ ๋น„๋™๊ธฐ ๋ฉ”์‹œ์ง• ํŒจ๋Ÿฌ๋‹ค์ž„์œผ๋กœ, ๋ฐœ์‹ ์ž์™€ ์ˆ˜์‹ ์ž๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ๋‹ค. ์ด ๋•Œ, ๋ฐœ์‹ ์ž์˜ ๋ฉ”์‹œ์ง€๋Š” ํŠน๋ณ„ํ•œ ์ˆ˜์‹ ์ž๊ฐ€ ์ •ํ•ด์ ธ์žˆ์ง€ ์•Š์•„๋„ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ๋ณด๋‚ด๋Š”๋ฐ ๋ฐ›๋Š” ๋…€์„์ด ์—†์–ด๋„ ๋œ๋‹ค๋Š” ๋ง์ด๋‹ค. ๋Œ€์‹  ์ˆ˜์‹ ํ•˜๋Š” ์ชฝ์—์„œ ๋“ฑ๋กํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค. ์ด ๋•Œ, ์ˆ˜์‹ ์ž๋Š” ์–ด๋–ค ๋ฐœ์‹ ์ž๋กœ๋ถ€ํ„ฐ ๋ฉ”์‹œ์ง€๊ฐ€ ์ „๋‹ฌ๋˜๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์•Œ ํ•„์š”๊ฐ€ ์—†๋‹ค. ์ฆ‰, ์ˆ˜์‹ ์ธก์˜ ์ •๋ณด๋ฅผ ์ „ํ˜€ ๋ชฐ๋ผ๋„ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ง์ด๋‹ค.

์‰ฌ์šด ์ดํ•ด๋ฅผ ์œ„ํ•ด, ์šฐ๋ฆฌ๊ฐ€ ์šฐํŽธ๋ฌผ์„ ๋ฐ›๋Š” ๋™์ž‘์„ ์ƒ๊ฐํ•ด๋ณด์ž. ๋ˆ„๊ตฐ๊ฐ€์™€์˜ ํŽธ์ง€๋ฅผ ๋‚˜๋ˆ„๋Š” ์ƒํ™ฉ์ด์—ˆ๋‹ค๋ฉด, A๋กœ๋ถ€ํ„ฐ ์—ฐ๋ฝ์ด ์˜ฌ ๊ฒƒ์„ ๊ธฐ๋Œ€ํ•˜๊ฒŒ ๋˜์ง€๋งŒ, ๋งŒ์•ฝ ๊ตญ๊ฐ€๋กœ๋ถ€ํ„ฐ ๋‚ ์•„์˜ค๋Š” ๋‚ฉ์„ธ ์šฐํŽธ(์œผ์œผ)์ด๋ผ๋ฉด ์–ธ์ œ ๋ˆ„๊ตฐ๊ฐ€์—๊ฒŒ ์˜ฌ ์ง€ ์˜ˆ์ƒํ•˜์ง€ ๋ชปํ•œ ์ƒํ™ฉ์—์„œ ๋ฐ›๊ฒŒ ๋œ๋‹ค. ์ฆ‰, ์ˆ˜์‹ ์ž์˜ ์ •๋ณด๋ฅผ ๊ตณ์ด ๋ชจ๋ฅด๋”๋ผ๋„ ๋‚ด ์šฐํŽธํ•จ์— ์šฐํŽธ์ด ๋„์ฐฉํ•ด์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ์šฐํŽธ์„ ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ ๋‚˜์˜ ์ฃผ์†Œ๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋“ฑ๋กํ•  ํ•„์š”๋Š” ์žˆ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€์˜ ์›๋ฆฌ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

Pros

๋Š์Šจํ•œ ๊ฒฐํ•ฉ์„ ๊ฐ€์ง„๋‹ค๋Š” ๊ฒƒ์ด ์žฅ์ ์œผ๋กœ ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค. ์œ„์—์„œ ๋งํ•œ ๊ฒƒ ์ฒ˜๋Ÿผ, ๊ฒฐ๊ตญ ์ˆ˜์‹ ์ž์˜ ์ž…์žฅ์—์„œ๋Š” ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ๋‚˜์—๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ผ์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ชฐ๋ผ๋„ ๋œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์—, ํ•ด๋‹น ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๋Š”๋ฐ ์žˆ์–ด์„œ ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ ์‰ฝ๊ฒŒ ๋Œ€์ฒด๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ฐ ๊ฐ์ฒด ํ˜น์€ ์„œ๋ฒ„ (์•ž์œผ๋กœ๋Š” ์ถ”์ƒ์  ๊ฐœ๋…์œผ๋กœ ํ† ํด๋กœ์ง€๋ผ ์นญํ•˜๊ฒ ๋‹ค)๋Š” ๋…๋ฆฝ์ ์œผ๋กœ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ ํด๋ผ์ด์–ธํŠธ-์„œ๋ฒ„ ํŒจ๋Ÿฌ๋‹ค์ž„์ด์—ˆ๋‹ค๋ฉด, ์„œ๋ฒ„ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š” ๋™์•ˆ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์—†์œผ๋ฉฐ, ํด๋ผ์ด์–ธํŠธ ์—ญ์‹œ ์‹คํ–‰๋˜์ง€ ์•Š์œผ๋ฉด ์ˆ˜์‹ ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ ์‹œ์Šคํ…œ ํ™•์žฅ์„ฑ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ ์ •ํ™•ํžˆ ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜์—ฌ ์ถ”๊ฐ€ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

Cons

ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๋Š์Šจํ•œ ๊ฒฐํ•ฉ(decoupling)์„ ๊ฐ€์ง„๋‹ค๋Š” ๊ฒƒ์ด ํฐ ๊ฒฐ์ ์œผ๋กœ ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฉ”์‹œ์ง€ ์ˆ˜์‹  ๊ฐ์ฒด๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” PublisherCenter๋Š” ๊ฒฐ๊ตญ ๋ฉ”์‹œ์ง€ ์ „๋‹ฌ์˜ ์„ฑ๊ณต ์—ฌ๋ถ€๋ฅผ ์•Œ ์ˆ˜ ์—†๋‹ค. ๋งŒ์•ฝ tightํ•œ coupling์„ ์ง€ํ–ฅํ•˜๋Š” ํŒจํ„ด์ด์—ˆ๋‹ค๋ฉด ์ด๋ฅผ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณด๋‹ค ์™„์ „์„ฑ์žˆ๋Š” ํ†ต์‹ ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด๋Š” ์ˆ˜์‹ ์ž ์ž…์žฅ์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง„๋ฐ, ๊ฒฐ๊ตญ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋ƒˆ๋Š”์ง€ ๊ทธ๋ ‡์ง€ ์•Š์€์ง€์— ๋Œ€ํ•œ ํŒ๋‹จ์„ ํ•  ์ˆ˜ ์—†๋‹ค.

๋˜ํ•œ Subscriber์™€ Publisher์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์•„์ง์— ๋”ฐ๋ผ, ๊ตํ™˜๋˜๋Š” ๋ฉ”์‹œ์ง€์˜ ์ˆ˜๊ฐ€ ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ ์•„ํ‚คํ…์ณ๊ฐ€ ๋ถˆ์•ˆํ•ด์ง„๋‹ค. ํ•œ๋ฒˆ์˜ ์ด๋ฒคํŠธ ๋ฐœ์†ก์— ๋”ธ๋ ค์žˆ๋Š” handler๋ฅผ ๋ชจ๋‘ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ˆ˜์‹ ์ž์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์•„์ง„๋‹ค๋ฉด, ์ด๋ฅผ ์—ฐ์†ํ•˜์—ฌ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ˆœ๊ฐ„์ ์œผ๋กœ ์‹œ์Šคํ…œ ๋ถ€ํ•˜๊ฐ€ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ๋‹ค.

๊ฐœ์ธ์ ์œผ๋กœ ์‚ฌ์šฉํ•ด๋ณด์•˜์„ ๋•Œ ์–ด๋ ค์›€์€, ์ „์—ญ์œผ๋กœ ๊ด€๋ฆฌ๋œ๋‹ค๋Š” ์ ์ด์—ˆ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ์ด ์–ด๋””์ธ์ง€ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด๋ฅผ postํ•˜๋Š” ๊ณณ์„ ์ฐพ์•„์•ผ ํ•˜๋Š”๋ฐ, decoupling์ด ์‹ฌํ•˜๋‹ค๋ณด๋‹ˆ ๋””๋ฒ„๊น…, ์œ ์ง€๋ณด์ˆ˜์— ์žˆ์–ด ์ข‹์ง€ ์•Š๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

์ถ”๊ฐ€์ ์ธ ์žฅ๋‹จ์ ์€ ํ•ด๋‹น ํŒจํ„ด์— ๋Œ€ํ•ด ๊นŠ์ด ๊ณต๋ถ€ํ•œ ์ดํ›„์— ํฌ์ŠคํŒ…์œผ๋กœ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

Notification Center

๊ฐ„๋‹จํ•˜๊ฒŒ ๋””์ž์ธ ํŒจํ„ด์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜์œผ๋‹ˆ, ์‹ค์ œ iOS์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์„ ์•Œ์•„๋ณด์ž.

ํฌ๊ฒŒ ๋‹ฌ๋ผ์ง„ ๊ฒƒ์€ ์—†๋‹ค. ๋‹ค๋งŒ ์šฉ์–ด๋“ค์ด ์กฐ๊ธˆ ๋‹ฌ๋ผ์กŒ๋‹ค. PublisherCenter๋Š” NotificationCenter๋กœ, Object๋Š” ๊ตฌ์ฒด์ ์œผ๋กœ Model, View์—์„œ ๋ณด๋‚ด๋Š” ๊ฒƒ์œผ๋กœ, ๊ทธ๋ฆฌ๊ณ  Subscriber๋Š” Observer๋กœ ๋ช…์นญ์ด ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค. ๊ธฐ๋ณธ์ ์ธ ์›๋ฆฌ๋Š” ๊ฐ™์œผ๋‹ˆ, ์‹ค์ œ๋กœ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

Custom Notification

class ViewController {
    // 1. Get NotificationCenter Instance  
    let notificationCenter = NotificationCenter.default
    
    override func viewDidLoad() {
        super.viewDidLoad()
      
        // 2. Custom Notification Name
        let notificationName = Notification.Name("NotificationName") 
        // 3. Add Observer - Register and Observe for changes
        notificationCenter.addObserver(self, // 2a. An object to register as an observer.
                                       selector: #selector(myMethod(_:)), // 2b. Method what will be called if Notification triggers
                                       name: notificationName, // 2c. Name of the Notification to know to Identify the message
                                       object: nil)
    }
    
    @objc func myMethod(_ notification: NSNotification) {
        // 4. The call
        // 4a. In case the message is passing some data it is in 'notification.userInfo'
    }
 
}
 
class Model {
    func postNotification() {
        let notificationName = Notification.Name("NotificationName")
        let message = ["message_key": "message_value"]
        Notification.default.post(name: notificationName, object: self, userInfo: message) // ๋ฐœ์‹ ์ž๋ฅผ ์•Œ๋ฆฌ๊ณ  ์‹ถ๋‹ค๋ฉด object์— ๋„ฃ์–ด์ฃผ์ž
    }
}
 

๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์„ ์†Œ๊ฐœํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค. ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. Notification Instance๋ฅผ ๋งŒ๋“ ๋‹ค.
  2. NotificationCenter์— ๋“ฑ๋กํ•  NotificationName instance๋ฅผ ๋งŒ๋“ ๋‹ค.
  3. Observeํ•˜์—ฌ ๋™์ž‘์„ ์‹คํ–‰ํ•˜๊ณ  ์‹ถ์€ ๊ฐ์ฒด์—์„œ Observer๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
    • selector ์‚ฌ์šฉ: objective-C runtime ์‚ฌ์šฉ, @objc ํ‚ค์›Œ๋“œ ์š”๊ตฌ๋จ, ๋™๊ธฐ ๋ฐฉ์‹
    • handler ์‚ฌ์šฉ: queue ์ง€์ •์„ ํ†ตํ•ด ๋น„๋™๊ธฐ ๋ฐฉ์‹ ์ ์šฉ ๊ฐ€๋Šฅ
  4. ์›ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. ๋ฐ์ดํ„ฐ๊ฐ€ ์ „์†ก๋˜์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ userInfo๋กœ ์ „๋‹ฌํ•œ๋‹ค.
  5. (์ฝ”๋“œ์—๋Š” ์—†์ง€๋งŒ) ์•ฑ ํƒ€๊ฒŸ์ด iOS 9.0, macOS 10.11 ์ด์ „์ด๋ผ๋ฉด ๋“ฑ๋กํ•œ observer๋ฅผ ์ œ๊ฑฐํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์ดํ›„์˜ ๊ฒฝ์šฐ system์ด ์ œ๊ฑฐํ•ด์ค€๋‹ค.
  6. ์ •๋ณด๋ฅผ ๋ณด๋‚ด๋Š” ์ชฝ์—์„œ๋Š” ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด post ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณด๋‚ธ๋‹ค.
@objc extension NSNotification {
    public static let myCustomNotificationName = Notification.Name.myCustomNotificationName
}

extension Notification.Name { }์œผ๋กœ ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์ „์—ญ ์Šค์ฝ”ํ”„๋กœ ๊ด€๋ฆฌ๋˜๋‹ˆ ๋ถ€๋ถ„์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ํ•ด๋‹น ๋ฐฉ๋ฒ•์€ ์ข‹์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค. ํ•ด๋‹น Name์„ ๊ด€๋ฆฌํ•ด์•ผ ํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ๋ˆ„๊ฐ€ ๋˜์–ด์•ผ ํ•˜๋Š”์ง€ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

NotificationCenter Sync

func addObserver(_ observer: Any, 
        selector aSelector: Selector, 
            name aName: NSNotification.Name?, 
          object anObject: Any?)

์œ„ ๋ฐฉ์‹์œผ๋กœ observer๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๊ฒƒ์—๋Š” ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค๊ณ  ํ–ˆ๋‹ค. ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ์‹์€ ๋™๊ธฐ ๋ฐฉ์‹์ด๋‹ค. ๊ฐ„ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

NotificationCenter Async

func addObserver(forName name: NSNotification.Name?, 
          object obj: Any?, 
           queue: OperationQueue?, 
           using block: @escaping (Notification) -> Void) -> NSObjectProtocol

Pub/Sub ํŒจํ„ด์—์„œ ๋งŽ์€ subscriber๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์„ ์‹œ ๋ถ€ํ•˜๊ฐ€ ๊ฑธ๋ฆด ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ ์—์„œ ๋น„๋™๊ธฐ์ ์ธ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์œ„์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. queue๊ฐ€ nil์ธ ๊ฒฝ์šฐ ๋™๊ธฐ์ ์œผ๋กœ ์ž‘๋™๋œ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜๋Š” selector๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋™๊ธฐ ๋ฉ”์„œ๋“œ์™€ ๋‹ค๋ฅด๊ฒŒ, return value๋ฅผ ๊ฐ€์ง„๋‹ค. ๋งŒ์•ฝ ์ค‘๊ฐ„์— observing์„ ๊ทธ๋งŒํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ํ•ด๋‹น return value๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ฐ€, center.removeObserver(self.observer)์™€ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์ œ๊ฑฐํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ ํ•œ๋ฒˆ๋งŒ observingํ•˜๊ณ  ๊ทธ๋งŒ ๋‘๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์„ ์ถ”์ฒœํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

let center = NSNotificationCenter.defaultCenter()
let mainQueue = NSOperationQueue.mainQueue()
var token: NSObjectProtocol?
token = center.addObserverForName("OneTimeNotification", object: nil, queue: mainQueue) { (note) in
    print("Received the notification!")
    center.removeObserver(token!)
}

OS provided Notification

iOS์—์„œ๋„ ์ด NotificationCenter๋ฅผ ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉ์ค‘์ด๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ์žˆ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด NSNotification.Name๋ฅผ ์ฐธ๊ณ ํ•˜์ž.

static let elementFocusedNotification: NSNotification.Name
static let boldTextStatusDidChangeNotification: NSNotification.Name 
class let didBecomeActiveNotification: NSNotification.Name
class let didReceiveMemoryWarningNotification: NSNotification.Name 
class let textDidChangeNotification: NSNotification.Name
class let batteryLevelDidChangeNotification: NSNotification.Name 
class let keyboardWillShowNotification: NSNotification.Name

์œ„์— ๋ณด์ด๋Š” didBecomeActiveNotification์˜ ๊ฒฝ์šฐ, AppDelegate์—์„œ์˜ applicationDidBecomeActive(_:) ํ˜น์€ SceneDelegate์˜ sceneDidBecomeActive(_:)์™€ ๋™์ผํ•˜๋‹ค. ๊ธฐ์กด์—๋Š” ๊ฐ๊ฐ์˜ ์†Œ์Šค ํŒŒ์ผ ๋‚ด๋ถ€์—์„œ ์ด๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ๋ฐ–์— ์—†์—ˆ๋‹ค๋ฉด, ์ด์ œ ์ด NotificationName์œผ๋กœ Observingํ•œ๋‹ค๋ฉด, OS๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์‹œ์ ์—, ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

Sum up

  • NotificationCenter๋Š” Pub/Sub pattern์„ ์‚ฌ์šฉํ•œ ๋ฉ”์‹œ์ง€ ์ „์†ก ๊ฐ์ฒด์ด๋‹ค.
  • Custom์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ , System์—์„œ ์ œ๊ณตํ•˜๋Š” ๋…€์„์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
  • selector๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ iOS 9.0 ์ด์ „์ด๋ผ๋ฉด observer๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์›Œ์ฃผ์–ด์•ผ ํ•œ๋‹ค.
  • ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” method๋„ ์กด์žฌํ•œ๋‹ค.

์˜ค๋Š˜์€ ๊ฐ„๋‹จํ•˜๊ฒŒ NotificationCenter์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค. ๋งŒ์•ฝ Combine ํ™œ์šฉ์ด ๊ถ๊ธˆํ•˜๋‹ค๋ฉด 03. Notification To Combine์„ ์ฝ์–ด๋ณด์ž. ๋!

Reference