Skip to content

Instantly share code, notes, and snippets.

@joshavant
Created April 17, 2014 22:04
Show Gist options
  • Save joshavant/11014146 to your computer and use it in GitHub Desktop.
Save joshavant/11014146 to your computer and use it in GitHub Desktop.
Handy little design pattern that lets you kick off processes in arbitrary objects + lets arbitrary dependencies add themselves into a notification chain

##XXAppDelegate.m, XXSomeView.m, some other object, etc##

...
[[XXSomeSingletonController sharedInstance] doThatThing];
...

##XXSomeSingletonControllerObserver.h##

@protocol XXSomeSingletonControllerObserver <NSObject>
- (void)didFinishThatThing;
@end

##XXSomeSingletonController.m##

- (void)doThatThing
{
  ... // this was the method we first called from XXAppDelegate, XXSomeView, etc
}

- (void)finishDoingThatThing
{
  [self.observers enumerateObjectsUsingBlock:^(id<XXSomeSingletonControllerObserver> observer, BOOL *stop) {
    if([observer respondsToSelector:@selector(didFinishThatThing)])
      [observer didFinishThatThing];
  }];
}

##XXViewController.m##

@interface XXViewController () <XXSomeSingletonControllerObserver>
@end

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  [[XXSomeSingletonController sharedInstance] addObserver:self];
}

- (void)didFinishThatThing
{
  <#code to handle finishing the Thing#>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment