Correct management of addObserverForName:object:queue:usingBlock:

后端 未结 4 506
礼貌的吻别
礼貌的吻别 2021-02-01 03:37

I\'m still new to blocks in objective-c and wondering if I have this psuedo code correct. I\'m not sure if it\'s enough to just remove the observer or if i have to call removeOb

4条回答
  •  一向
    一向 (楼主)
    2021-02-01 04:20

    Apple Document about this method:

    The following example shows how you can register to receive locale change notifications.

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    self.localeChangeObserver = [center addObserverForName:NSCurrentLocaleDidChangeNotification object:nil
        queue:mainQueue usingBlock:^(NSNotification *note) {
    
            NSLog(@"The user's locale changed to: %@", [[NSLocale currentLocale] localeIdentifier]);
        }];
    

    To unregister observations, you pass the object returned by this method to removeObserver:. You must invoke removeObserver: or removeObserver:name:object: before any object specified by addObserverForName:object:queue:usingBlock: is deallocated.

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center removeObserver:self.localeChangeObserver];
    

提交回复
热议问题