Skip to content

A helper class to handle multiple completion handlers for a task

License

Notifications You must be signed in to change notification settings

dtorres/BatchedHandlers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Batched Handlers

Easily batch handlers while doing the work once and call all of them as if you were calling one.

Usage

//Create a manager
self.manager = [BHManager new];

//––––––––––––––––––––––––––––––––––––––––––––––––––
//Later when a method with a handler is called...
//––––––––––––––––––––––––––––––––––––––––––––––––––
- (void)doAsyncWork:(void(^)(id result, NSError *error)handler
{
  //Any cache checking goes here...
  
  //The manager returns an array that will contain all the handlers to call.
  NSArray *handlers = BHManagerAddHandler(self.manager,handler,NSStringFromSelector(_cmd)); //We use the selector as a key
  //Continue only if an array is returned 
  if (!handlers) {
    return;
  }

  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //Do async work
    for(typeof(handler) aHandler in handlers) {
      aHandler(result, error);
    }
  });
}

In case you want to accept NULL handlers, you must register a sample block beforehand:

//Note this block is never executed nor does it need to do anything, 
//just be of the same interface as the passed handlers

[self.manager setSampleHandler:^(id r, NSError *e){}
              forKey:NSStringFromSelector(@selector(doAsyncWork:))];

Installation

BatchedHandlers is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "BatchedHandlers"

About

A helper class to handle multiple completion handlers for a task

Resources

License

Stars

Watchers

Forks

Packages

No packages published