Talk is cheap, show me the code.
- 在工具类中处理业务逻辑
#import <Foundation/Foundation.h>
#import "GCDMulticastDelegate.h"
@protocol ManagerDelegate<NSObject>
- (void)doSomething;
@end
@interface Manager : NSObject
{
GCDMulticastDelegate<ManagerDelegate> *_delegates;
}
+ (instancetype)sharedManager;
- (void)letusDoSomething;
- (void)_addDelegate:(id<ManagerDelegate>)delegate delegateQueue:(dispatch_queue_t)queue;
- (void)removeDelegate:(id<ManagerDelegate>)delegate;
@end
复制代码
#import "Manager.h"
static Manager *manager = nil;
static NSObject *g_lock = nil;
@implementation Manager
+ (instancetype)sharedManager
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[Manager alloc] init];
});
return manager;
}
- (instancetype)init
{
self = [super init];
if (self) {
_delegates = (GCDMulticastDelegate<ManagerDelegate> *)[[GCDMulticastDelegate alloc] init];
g_lock = [[NSObject alloc] init];
}
return self;
}
- (void)_addDelegate:(id<ManagerDelegate>)delegate delegateQueue:(dispatch_queue_t)queue
{
@synchronized(g_lock) {
if (!queue) {
queue = dispatch_get_main_queue();
}
[_delegates addDelegate:delegate delegateQueue:queue];
}
}
- (void)removeDelegate:(id<ManagerDelegate>)delegate
{
if (!delegate) {
return;
}
@synchronized(g_lock) {
[_delegates removeDelegate:delegate];
}
}
- (void)letusDoSomething
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), queue, ^{
[_delegates doSomething];
});
}
@end
复制代码
- 在
Appdelegate
和ViewController
中都遵守<ManagerDelegate>
并实现协议中声明的方法
@interface AppDelegate ()<ManagerDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[Manager sharedManager] _addDelegate:self delegateQueue:nil];
return YES;
}
- (void)doSomething
{
NSThread *thread = [NSThread currentThread];
NSLog(@"Appdelegate:%@",thread);
}
复制代码
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_queue_t qt = dispatch_queue_create("testmd.f.f", DISPATCH_QUEUE_SERIAL);
[[Manager sharedManager] _addDelegate:self delegateQueue:qt];
}
- (void)doSomething {
NSThread *thread = [NSThread currentThread];
NSLog(@"VC:%@",thread);
}
- (void)dealloc {
NSLog(@"dealloc");
}
复制代码
- 在VC的
touchBegin
方法中调用letusDoSomething
方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[[Manager sharedManager] letusDoSomething];
}
复制代码
5s后打印信息如下:
2018-04-27 16:22:30.708421+0800 GCDMulticastDelegateDemo[1627:206523] Appdelegate:<NSThread: 0x60400006b580>{number = 1, name = main}
2018-04-27 16:22:30.708423+0800 GCDMulticastDelegateDemo[1627:206644] VC:<NSThread: 0x600000262080>{number = 3, name = (null)}
复制代码
- 在业务逻辑执行期间把
delegate
移除掉
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[[Manager sharedManager] letusDoSomething];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[Manager sharedManager] removeDelegate:self];
});
}
复制代码
打印信息如下:
2018-04-27 16:27:06.424868+0800 GCDMulticastDelegateDemo[1652:217365] Appdelegate:<NSThread: 0x604000076000>{number = 1, name = main}
复制代码
-
github地址