Floc-Dispatcher 0.1.2

Floc-Dispatcher 0.1.2

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Unclaimed.


Downloads

Total4
Week0
Month0

Installs

Apps1
powered by Segment

GitHub

Stars6
Watchers1
Forks0
Issues0
Contributors1
Pull Requests0

Code

Files4
LOCLines of Code 165



  • By
  • Simon Schmid

Floc Dispatcher

Floc Dispatcher Logo

Description

An alternative to NSNotificationCenter.

Features

  • Dispatch any object (no NSNotification like in NSNotificationCenter)
  • Add observers with priority
  • Add observers that get removed after execution (add once)

How to use Floc Dispatcher

Get a dispatcher

// Create your own dispatcher
GDDispatcher *dispatcher = [[GDDispatcher alloc] init];

// or use the shared dispatcher
GDDispatcher *dispatcher = [GDDispatcher sharedDispatcher];

Add observers

[dispatcher addObserver:self forObject:[Greeting class]
           withSelector:@selector(doSthLast:) priority:-5];

[dispatcher addObserver:self forObject:[Greeting class]
           withSelector:@selector(doSthFirst:) priority:10];

You can add observers that get removed after execution

[dispatcher addObserverOnce:self forObject:[Greeting class]
           withSelector:@selector(doSthLast:) priority:-5];

[dispatcher addObserverOnce:self forObject:[Greeting class]
           withSelector:@selector(doSthFirst:) priority:10];

Dispatch objects

[dispatcher dispatchObject:[[Greeting alloc] initWithString:@"Hello"]];

// Logs
// Got greeting first: Hello
// Got greeting last: Hello
- (void)doSthFirst:(Greeting *)greeting {
    NSLog(@"Got greeting first: %@", greeting.string);
}

- (void)doSthLast:(Greeting *)greeting {
    NSLog(@"Got greeting last: %@", greeting.string);
}

Example

In this example a fake service simulates fetching some data from a remote server. It dispatches the response. An other class is observing objects of type User. If an User gets dispatched somewhere in the application, the observer gets notified and the target selector gets performed. fl_dispatcher_add, fl_dispatcher_remove and fl_dispatcher_dispatch are some convenience macros available in Floc Dispatcher.

@interface Example ()
@property(nonatomic, strong) Service *service;
@end

@implementation Example

- (id)init {
    self = [super init];
    if (self) {
        fl_dispatcher_add(self, [User class], @selector(updateWithUser:));

        self.service = [[Service alloc] init];
        [self.service login];
    }

    return self;
}

- (void)updateWithUser:(User *)user {
    NSLog(@"user.name = %@", user.name);
    NSLog(@"user.age = %u", user.age);
}

- (void)dealloc {
    fl_dispatcher_remove(self);
}

@end
@implementation Service

- (void)login {
    [self performSelector:@selector(remoteServerResponded) withObject:nil afterDelay:0.5];
}

- (void)remoteServerResponded {
    User *user = [[User alloc] init];
    user.name = @"Joe";
    user.age = 28;
    fl_dispatcher_dispatch(user);
}

@end

Install Floc Dispatcher

You find the source files you need in Floc-Dispatcher/Classes.

Install Floc Dispatcher

$ cd path/to/project
$ pod install

Open the created Xcode Workspace file.