通知中心

一愉豺、主線程開啟通知中心

在主線程發(fā)送通知是同步的允乐,執(zhí)行順序 before矮嫉、ing、after牍疏。

- (void)viewDidLoad {
    [super viewDidLoad];
    //注冊通知
    //object傳nil蠢笋,表示,接受所有名字為notifyName的通知鳞陨,否則只接受指定object對象的通知昨寞。
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotifi:) name:@"notifyName" object:nil];
}
- (void)handleNotifi:(NSNotification*)notifi{
    NSLog(@"ing");
    NSLog(@"%s",__func__);
}
- (IBAction)btnClicked:(id)sender {
    NSLog(@"before");
    //發(fā)送消息
    [[NSNotificationCenter defaultCenter] postNotificationName:@"notifyName" object:nil];
    NSLog(@"after");
}

在dealloc中移除通知中心。注:在控制器中注冊了一個消息中心,可以不用在dealloc中移除通知援岩,因為控制器自動執(zhí)行了這一操作歼狼。如果,自己創(chuàng)建一個類享怀,繼承于NSObject類羽峰,需要手動移除通知,否則會在iOS9以下的系統(tǒng)里崩潰凹蜈。

//移除所有的通知
- (void)removeObserver:(id)observer;
//移除指定的通知
- (void)removeObserver:(id)observer name:(nullable NSNotificationName)aName object:(nullable id)anObject;

二、子線程中開啟通知中心

執(zhí)行的順序是before忍啸、after仰坦、ing。

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotifi:) name:@"notifyName" object:nil];   
}
- (void)handleNotifi:(NSNotification *)notify{
    NSLog(@"ing");
}
- (IBAction)btnClicked:(id)sender {
    NSLog(@"before");
    [NSThread detachNewThreadWithBlock:^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"notifyName" object:nil userInfo:@{@"name":@"ads"}];
    }];
    NSLog(@"after");
}

三计雌、在主線程中用通知隊列的形式開啟通知

執(zhí)行順序:before悄晃、after、ing

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotifi:) name:@"notifyName" object:nil];
}
- (void)handleNotifi:(NSNotification *)notify{
    NSLog(@"ing = %@",[NSThread currentThread]);
}
- (IBAction)btnClicked:(id)sender {
    [self notiQueue];
}
- (void)notiQueue{
    NSLog(@"before");   
    NSNotificationQueue * notiQueue = [NSNotificationQueue defaultQueue];
    NSNotification * noti = [NSNotification notificationWithName:@"notifyName" object:self userInfo:@{@"userInfo":@"name1"}];
    //將通知放入隊列中凿滤,先進先出FIFO
    //NSPostWhenIdle 空閑時發(fā)送
    //NSPostASAP as soon as posible 盡快發(fā)送 當前runloop不是同一個mode,現(xiàn)等待一個runloop模式完后妈橄,就會執(zhí)行通知事件
    //NSPostNow 馬上執(zhí)行
    [notiQueue enqueueNotification:noti postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:nil];
    NSLog(@"after");
}

四、在子線程中用通知隊列開啟通知

執(zhí)行順序:before翁脆、after
怎么不執(zhí)行handleNotifi呢眷蚓?原因:runloop沒有資源。

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotifi:) name:@"notifyName" object:nil];
}
- (void)handleNotifi:(NSNotification *)notify{
    NSLog(@"ing = %@",[NSThread currentThread]);
}
- (IBAction)btnClicked:(id)sender {
    [NSThread detachNewThreadWithBlock:^{
        [self notiQueue];
    }];
}
- (void)notiQueue{
    NSLog(@"before");   
    NSNotificationQueue * notiQueue = [NSNotificationQueue defaultQueue];
    NSNotification * noti = [NSNotification notificationWithName:@"notifyName" object:self userInfo:@{@"userInfo":@"name1"}];
    //將通知放入隊列中反番,先進先出FIFO
    //NSPostWhenIdle 空閑時發(fā)送
    //NSPostASAP as soon as posible 盡快發(fā)送 當前runloop不是同一個mode,現(xiàn)等待一個runloop模式完后沙热,就會執(zhí)行通知事件
    //NSPostNow 馬上執(zhí)行
    [notiQueue enqueueNotification:noti postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:[NSArray arrayWithObjects:NSDefaultRunLoopMode,nil]];
    NSLog(@"after");
}

將runloop中添加資源,NSLog(@"runloop over"); 永遠不會被執(zhí)行罢缸。每次點擊按鈕篙贸,開啟通知,每次點擊都會多產(chǎn)生一條線程枫疆,這樣子會產(chǎn)生資源浪費爵川。
執(zhí)行順序:before、after息楔、ing寝贡。

- (void)notiQueue{
    NSLog(@"before");
    NSNotificationQueue * notiQueue = [NSNotificationQueue defaultQueue];
    NSNotification * noti = [NSNotification notificationWithName:@"notifyName" object:self userInfo:@{@"userInfo":@"name1"}];
    //將通知放入隊列中,先進先出FIFO
    //NSPostWhenIdle 空閑時發(fā)送
    //NSPostASAP as soon as posible 盡快發(fā)送 當前runloop不是同一個mode,現(xiàn)等待一個runloop模式完后值依,就會執(zhí)行通知事件
    //NSPostNow 馬上執(zhí)行
    [notiQueue enqueueNotification:noti postingStyle:NSPostWhenIdle coalesceMask:NSNotificationCoalescingOnName forModes:[NSArray arrayWithObjects:NSDefaultRunLoopMode,nil]];
    NSLog(@"after");
    NSPort * port = [[NSPort alloc] init];
    [[NSRunLoop currentRunLoop] addPort:port forMode:NSRunLoopCommonModes];   
    [[NSRunLoop currentRunLoop] run];
    NSLog(@"runloop over");
}

五兔甘、在指定線程上處理

通過NSPort讓通知在指定線程上執(zhí)行呢?如果將NSPort 放在主線程鳞滨,那么通知就會在主線程上執(zhí)行洞焙,將NSPort加在子線程上通知事件就會在子線程。

- (void)viewDidLoad {
    [super viewDidLoad];
    myPort = [[NSPort alloc] init];
    [myPort setDelegate:self];
    //放到子線程上
    [NSThread detachNewThreadWithBlock:^{
        [[NSRunLoop currentRunLoop] addPort:myPort forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
    }];
    //放在主線程上
    //[[NSRunLoop currentRunLoop] addPort:myPort forMode:NSDefaultRunLoopMode];
}
- (void)handlePortMessage:(NSPortMessage *)message{
    NSObject * obj = (NSObject *)message;
    NSLog(@"%@",[NSThread currentThread]);
    NSLog(@"message = %@",[obj valueForKey:@"msgid"]);
}
- (void)myPort{
    [myPort sendBeforeDate:[NSDate date] msgid:111 components:nil from:nil reserved:0];
}
- (IBAction)btnClicked:(id)sender {
    [self myPort];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市澡匪,隨后出現(xiàn)的幾起案子熔任,更是在濱河造成了極大的恐慌,老刑警劉巖唁情,帶你破解...
    沈念sama閱讀 221,430評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件疑苔,死亡現(xiàn)場離奇詭異,居然都是意外死亡甸鸟,警方通過查閱死者的電腦和手機惦费,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,406評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來抢韭,“玉大人薪贫,你說我怎么就攤上這事】坦В” “怎么了瞧省?”我有些...
    開封第一講書人閱讀 167,834評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長鳍贾。 經(jīng)常有香客問我鞍匾,道長,這世上最難降的妖魔是什么骑科? 我笑而不...
    開封第一講書人閱讀 59,543評論 1 296
  • 正文 為了忘掉前任橡淑,我火速辦了婚禮,結(jié)果婚禮上咆爽,老公的妹妹穿的比我還像新娘梳码。我一直安慰自己,他們只是感情好伍掀,可當我...
    茶點故事閱讀 68,547評論 6 397
  • 文/花漫 我一把揭開白布掰茶。 她就那樣靜靜地躺著,像睡著了一般蜜笤。 火紅的嫁衣襯著肌膚如雪濒蒋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,196評論 1 308
  • 那天把兔,我揣著相機與錄音沪伙,去河邊找鬼。 笑死县好,一個胖子當著我的面吹牛围橡,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播缕贡,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼翁授,長吁一口氣:“原來是場噩夢啊……” “哼拣播!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起收擦,我...
    開封第一講書人閱讀 39,671評論 0 276
  • 序言:老撾萬榮一對情侶失蹤贮配,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后塞赂,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體泪勒,經(jīng)...
    沈念sama閱讀 46,221評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,303評論 3 340
  • 正文 我和宋清朗相戀三年宴猾,在試婚紗的時候發(fā)現(xiàn)自己被綠了圆存。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,444評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡仇哆,死狀恐怖沦辙,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情税产,我是刑警寧澤怕轿,帶...
    沈念sama閱讀 36,134評論 5 350
  • 正文 年R本政府宣布偷崩,位于F島的核電站辟拷,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏阐斜。R本人自食惡果不足惜衫冻,卻給世界環(huán)境...
    茶點故事閱讀 41,810評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望谒出。 院中可真熱鬧隅俘,春花似錦、人聲如沸笤喳。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽杀狡。三九已至蒙畴,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間呜象,已是汗流浹背膳凝。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留恭陡,地道東北人蹬音。 一個月前我還...
    沈念sama閱讀 48,837評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像休玩,于是被迫代替她去往敵國和親著淆。 傳聞我的和親對象是個殘疾皇子劫狠,可洞房花燭夜當晚...
    茶點故事閱讀 45,455評論 2 359

推薦閱讀更多精彩內(nèi)容