不用自动移除的通知中心

Posted YouXianMing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了不用自动移除的通知中心相关的知识,希望对你有一定的参考价值。

自动移除的通知中心

 

源码

//
//  DefaultNotificationCenter.h
//  TotalCustomTabBarController
//
//  Created by YouXianMing on 16/6/3.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import <Foundation/Foundation.h>
@class DefaultNotificationCenter;

@protocol DefaultNotificationCenterDelegate <NSObject>

@required

/**
 *  DefaultNotificationCenter\'s event.
 *
 *  @param notification DefaultNotificationCenter object.
 *  @param name         Event name.
 *  @param object       Event object, maybe nil.
 */
- (void)defaultNotificationCenter:(DefaultNotificationCenter *)notification name:(NSString *)name object:(id)object;

@end

@interface DefaultNotificationCenter : NSObject

/**
 *  Post event to notification name.
 *
 *  @param name   Notification name.
 *  @param object Data.
 */
+ (void)postEventToNotificationName:(NSString *)name object:(id)object;

/**
 *  DefaultNotificationCenter\'s delegate.
 */
@property (nonatomic, weak) id <DefaultNotificationCenterDelegate>  delegate;

/**
 *  Add notification name.
 *
 *  @param name Notification name.
 */
- (void)addNotificationName:(NSString *)name;

/**
 *  Delete notification name.
 *
 *  @param name Notification name.
 */
- (void)deleteNotificationName:(NSString *)name;

/**
 *  Get all the notification names.
 *
 *  @return Notification names\'s array.
 */
- (NSArray <NSString *> *)notificationNames;

/**
 *  Remove all notifications.
 */
- (void)removeAllNotifications;

@end
//
//  DefaultNotificationCenter.m
//  TotalCustomTabBarController
//
//  Created by YouXianMing on 16/6/3.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "DefaultNotificationCenter.h"

@interface DefaultNotificationCenterModel : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic)         BOOL      effective;

@end

@implementation DefaultNotificationCenterModel

@end

@interface DefaultNotificationCenter ()

@property (nonatomic, strong) NSMutableArray <DefaultNotificationCenterModel *> *stringsArray;

@end

@implementation DefaultNotificationCenter

- (instancetype)init {
    
    if (self = [super init]) {
        
        self.stringsArray = [NSMutableArray array];
    }
    
    return self;
}

+ (void)postEventToNotificationName:(NSString *)name object:(id)object {

    [[NSNotificationCenter defaultCenter] postNotificationName:name object:object];
}

- (void)addNotificationName:(NSString *)name {

    // Check have the same name or not.
    __block BOOL haveTheSameName = NO;
    
    [self.stringsArray enumerateObjectsUsingBlock:^(DefaultNotificationCenterModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ([obj.name isEqualToString:name]) {
            
            haveTheSameName = YES;
            *stop           = YES;
        }
    }];
    
    // Add notification.
    if (haveTheSameName == NO) {
        
        DefaultNotificationCenterModel *model = [DefaultNotificationCenterModel new];
        model.name                            = name;
        model.effective                       = YES;
        [self.stringsArray addObject:model];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationEvent:) name:model.name object:nil];
    }
}

- (void)deleteNotificationName:(NSString *)name {
    
    // Check have the same name or not.
    __block BOOL      haveTheSameName = NO;
    __block NSInteger index           = 0;
    
    [self.stringsArray enumerateObjectsUsingBlock:^(DefaultNotificationCenterModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ([obj.name isEqualToString:name]) {
            
            haveTheSameName = YES;
            index           = idx;
            *stop           = YES;
        }
    }];
    
    // Remove notification.
    if (haveTheSameName == YES) {
        
        DefaultNotificationCenterModel *model = self.stringsArray[index];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:model.name object:nil];
        [self.stringsArray removeObject:model];
    }
}

- (NSArray <NSString *> *)notificationNames {

    NSMutableArray *namesArray = [NSMutableArray array];
    [self.stringsArray enumerateObjectsUsingBlock:^(DefaultNotificationCenterModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [namesArray addObject:obj.name];
    }];
    
    return [NSArray arrayWithArray:namesArray];
}

- (void)removeAllNotifications {

    [self.stringsArray enumerateObjectsUsingBlock:^(DefaultNotificationCenterModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:obj.name object:nil];
    }];
    
    [self.stringsArray removeAllObjects];
}

- (void)notificationEvent:(id)obj {

    NSNotification *notification = obj;

    if (self.delegate && [self.delegate respondsToSelector:@selector(defaultNotificationCenter:name:object:)]) {
        
        [self.delegate defaultNotificationCenter:self name:notification.name object:notification.object];
    }
}

- (void)dealloc {

    [self removeAllNotifications];
}

@end

 

细节

1. 将通知的接收转换成了代理,根据代理中的一个通知名字值来区分不同的通知.

2. 不用自动移除注册的通知

3. 用这个方法发送通知

 

以上是关于不用自动移除的通知中心的主要内容,如果未能解决你的问题,请参考以下文章

为啥通知会自动从 iOS 通知中心移除?

NSNotificatinonCenter iOS9以后不再需要移除观察者-备忘

Android Studio 添加已经移除的Module

移除通知的时机

iOS不移除通知也不会崩溃原因

应用打开后从通知中心移除推送