Git Product home page Git Product logo

eemultidelegate's Introduction

EEMultiProxy

support platform

A multicast-delegate class with thread-safe

Cocoa Pods

pod 'EEMultiDelegate', :git => "https://github.com/ienho/EEMultiDelegate.git"

or

pod 'EEMultiDelegate'

Example

  1. Let MessageService support multicast-delegate use EEMultiProxy
#import <Foundation/Foundation.h>

@protocol MessageReceiveDelegate <NSObject>

- (void)receiveMessage:(NSString *)message;

@end

@interface MessageService : NSObject

- (void)stopReceive;
- (void)receiveNewMessage;

@end
#import "MessageService.h"
#import <EEMultiDelegate/NSObject+EEMultiProxyAddition.h>

@implementation MessageService {
    dispatch_source_t _timer;
}

- (void)stopReceive {
    if (_timer) {
        dispatch_cancel(_timer);
        _timer = nil;
    }
}

- (void)receiveNewMessage {
    static NSInteger count = 0;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        NSString *newMessage = [NSString stringWithFormat:@"message %ld", (long)count];
        NSLog(@"--- --- --- --- --- --- --- --- --- --- --- --- --- ---");
        NSLog(@"the service receiving new message : %@", newMessage);
        [EEProxy(MessageReceiveDelegate) receiveMessage:newMessage];
        count++;
    });
    dispatch_resume(_timer);
}

@end
  1. MessagePanel implement delegate MessageReceiveDelegate
@implementation MessagePanel

- (instancetype)initWithName:(NSString *)name service:(MessageService *)service {
    if (self = [super init]) {
        _name = [name copy];
        [service ee_addDelegate:self];
    }
    return self;
}

- (void)showMessage:(NSString *)message {
    NSLog(@"MessagePanel-%@ show message : %@", _name, message);
}

#pragma mark - MessageReceiveDelegate

- (void)receiveMessage:(NSString *)message {
    [self showMessage:message];
}

@end
  1. the _panel1, _panel2, _panel3 will be invoke method receiveMessage.
_service = MessageService.new;

_panel1 = [[MessagePanel alloc] initWithName:@"A" service:_service];
_panel2 = [[MessagePanel alloc] initWithName:@"B" service:_service];
_panel3 = [[MessagePanel alloc] initWithName:@"C" service:_service];

[_service receiveNewMessage];

eemultidelegate's People

Contributors

grayland119 avatar ienho avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.