如何使用delegate进行一对多通信

在开发IOS应用的时候,一个类与另一个类之间的通信方式通常有四种模式:

1、委托delegation
2、通知中心Notification Center
3、block
4、键值观察key value observing,KVO
但是,有时候我们会出现一个类同时和多个类进行通信的情况,这时候大家通常会使用Notification,它可以一对多进行通信;但是Notification有一定的局限性,例如:1.不能从观察者获得任何的反馈信息(相比于delegate);2.Notification不关心接受者的态度,只管把通告放出来;如果使用delegate就可以避免这些不足之处;

delegation 实现一对多通信:

MessageSingleton.h (我为了方便演示使用了单例)

#import 

NS_ASSUME_NONNULL_BEGIN
@class MessageSingleton;
@protocol MessageSingletonDelegate 

- (void)messageSingleton:(MessageSingleton*) messageSingleton logNowDate:(NSDate*)nowDate;

- (void)messageSingleton:(MessageSingleton*) messageSingleton updateName:(NSString*)nameStr;

@end

@interface MessageSingleton : NSObject
+ (instancetype)sharedInstance;

/**
 *  添加单个 delegate 到MessageSingleton.
 */
- (void)addDelegate:(id) messageSingletonDelegate;

/**
 *  从MessageSingleton里移除一个delegate
 */
- (void)removeDelegate:(id)   messageSingletonDelegate;

/**
 *  移除所有添加过的delegate.
 */
- (void)removeAllDelegates;
@end

NS_ASSUME_NONNULL_END

MessageSingleton.m

#import "MessageSingleton.h"

@interface MessageSingleton()
/**
*  用来存储所有添加j过的delegate
*  NSHashTable 与 NSMutableSet相似,但NSHashTable可以持有元素的弱引用,而且在对象被销毁后能正确地将其移除。
*/
@property (strong, nonatomic, readonly) NSHashTable                     *delegates;

/**
*  delegateLock 用于给delegate的操作加锁,防止多线程同时调用
*/
@property (strong, nonatomic) NSLock                                    *delegateLock;
@end

@implementation MessageSingleton
+ (instancetype)sharedInstance {
   static MessageSingleton *messageSingleton = nil;
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
       messageSingleton = [[self alloc] init];
   });
   return messageSingleton;
}

#pragma mark - get & set
- (instancetype)init {
   self = [super init];
   if (self) {
       _delegates = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
       //使用timer模式调用delegate
       NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
           [self callMessageSingletonLogNowDateDelegate];
           [self callMessageSingletonUpdateNameDelegate];
       }];
       
       [timer fire];
   }
   return self;
}

- (NSLock *)delegateLock {
   if (_delegateLock == nil) {
       _delegateLock = [[NSLock alloc] init];
   }
   return _delegateLock;
}

#pragma mark - delegate
- (void)addDelegate:(id) messageSingletonDelegate {
   [self.delegateLock lock];//防止多线程同时调用
   [self.delegates addObject:messageSingletonDelegate];
   [self.delegateLock unlock];
}

- (void)removeDelegate:(id) messageSingletonDelegate {
   [self.delegateLock lock];//防止多线程同时调用
   [self.delegates removeObject:messageSingletonDelegate];
   [self.delegateLock unlock];
}

- (void)removeAllDelegates {
   [self.delegateLock lock];//防止多线程同时调用
   [self.delegates removeAllObjects];
   [self.delegateLock unlock];
}

#pragma mark - call delegates
- (void)callMessageSingletonLogNowDateDelegate {
   [self.delegateLock lock];
   for (id delegate in self.delegates) {//遍历delegates ,call delegate
       if ([delegate respondsToSelector:@selector(messageSingleton:logNowDate:)]) {
           [delegate messageSingleton:self logNowDate:[NSDate date]];
       }
   }
   [self.delegateLock unlock];
}

- (void)callMessageSingletonUpdateNameDelegate {
   [self.delegateLock lock];
   for (id delegate in self.delegates) {
       if ([delegate respondsToSelector:@selector(messageSingleton:updateName:)]) {
           [delegate messageSingleton:self updateName:@"奋斗的小鸟"];
       }
   }
   [self.delegateLock unlock];
}

@end

在A类中实现MessageSingletonDelegate:

#import "ViewController.h"
#import "MessageSingleton.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[MessageSingleton sharedInstance] addDelegate:self];
}

- (void)messageSingleton:(MessageSingleton*) messageSingleton logNowDate:(NSDate*)nowDate {
    NSLog(@"logNowDate Delegate :%@",nowDate);
}

- (void)messageSingleton:(MessageSingleton*) messageSingleton updateName:(NSString*)nameStr {
    NSLog(@"updateName Delegate :%@",nameStr);
}

@end

同时在B类中也实现MessageSingletonDelegate:

#import "NextViewController.h"
#import "MessageSingleton.h"
@interface NextViewController ()

@end

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[MessageSingleton sharedInstance] addDelegate:self];
}

- (void)dealloc {
    [[MessageSingleton sharedInstance] removeDelegate:self];
}

- (void)messageSingleton:(MessageSingleton*) messageSingleton logNowDate:(NSDate*)nowDate {
    NSLog(@"NextVC logNowDate Delegate :%@",nowDate);
}

- (void)messageSingleton:(MessageSingleton*) messageSingleton updateName:(NSString*)nameStr {
    NSLog(@"NextVC updateName Delegate :%@",nameStr);
}

你可能感兴趣的:(如何使用delegate进行一对多通信)