Lined Notebook

NotificationCenter 동작 방식과 활용 방안에 대해 설명하시오.

by 사슴비행기

NotificationCenter 동작 방식

등록된 모든 Observer에게 정보를 전달하는 매커니즘.

 

 

 

 

Observer(Listener) : Notification을 관찰 감지

Sender : 필요 시 Notification을 해당 Observer에게 보냄

itself : NotificationCenter 그 자체

 

==> 먼저, 옵저버를 등록하면, 등록된 옵저버가 관찰하면서 변경사항이 발생하면 Sender가 등록된 옵저버에 알림을 보내고, 옵저버 등록 시 설정한 메서드가 실행된다.

 

1. 옵저버 등록

2. 옵저버는 관찰 시작

3. (옵저버 등록 시 설정한) 특정 작업이 발생 시 Sender가 Post

4. (옵저버 등록 시 설정한) Observer selector 실행

 

NotificationCenter.default.addObserver(self, selector: #selector(handleNoti(_:)), name: myNoti, object: nil)
NotificationCenter.default.post(name: "myNoti", object: "전달할 값")

 

 

사용예시

* 1번 화면에서 다운로드를 실행하고 2번 화면으로 넘어갔을 때 다운로드가 완료되면, 다운로드 알림 팝업을 띄울 수 있다.

* 서버에서 데이터를 내려 받고 완료 시, 화면을 reflash 할 수 있다.

* 키보드가 화면을 가릴 때 post를 보내서 스크롤를 움직여서 가림 현상을 막을 수 있다.

 

 

참고: 

 

NotificationCenter 동작 방식과 활용 방안에 대해 설명하시오 | Notion

NotificationCenter

wandering-office-654.notion.site

 

NotificationCenter 동작 방식과 활용 방안에 대해 설명하시오.

NotificationCenter는 등록된 모든 Observer에게 정보를 전달하는 메커니즘입니다. observer는 notification들을 감지하고 있고 sender는 필요할 때 해당 observer에게 notification들을 보내주는 역할을 합니다. 옵저

velog.io

 

블로그의 정보

Beautiful Coding

사슴비행기

활동하기