Notification
#Notification #NSNotification
概要
Notification とは、ある情報をブロードキャスト、及びその情報を購読するし受け取るためのデザインパターンとして、Foundation Framework 内に含まれているもの。
Notifications - Foundation | Apple Developer Documentation
Notification Programming Topics
登場人物
NotificationCenter
Notification を dispatch する責務を担う
単一のプログラム内でのみ dispatch が可能であり、他のプロセス間とやり取りしたい場合は DistributedNotificationCenter を利用する
default の NotificationCenter が用意されている
notification 発生時には、NotificationCenter が自身に登録されている Observer 群のリストを舐める
このリストを舐める操作が、Notification が多用されていると遅くなっていくため、大規模に Notification を利用しているようなアプリの場合は、複数の NotificationCenter を利用することでパフォーマンスのスループットを向上させることができる
NotificationCenter - Foundation | Apple Developer Documentation
Notification
実際に通知が発生した場合に受け取る情報のコンテナ
名前の一覧が定義されている
NSNotification.Name
Notification - Foundation | Apple Developer Documentation
利用方法
基本的には、NotificationCenter に以下を登録すれば、通知を受け取ることができる。
通知したい通知名
通知を受け取る Object or Closure
特定の Object の、Notification を引数にとる特定のメソッド
(特定の Dispatch Queue における) Notification を引数にとる特定の Closure
(Option) 通知元のオブジェクト
通知名だけでなく、特定のオブジェクトからの通知であることも指定したい場合に指定可能
これを指定しなかった場合、全ての該当通知名が通知される