Skip to content

Instantly share code, notes, and snippets.

@bvanderveen
Created July 9, 2011 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvanderveen/1073329 to your computer and use it in GitHub Desktop.
Save bvanderveen/1073329 to your computer and use it in GitHub Desktop.
Async workflows for Obj-C
@protocol AsyncProducer;
@protocol AsyncConsumer <NSObject>
- (void)producer:(id<AsyncProducer>)producer didCompleteWithResult:(id)result;
- (void)producer:(id<AsyncProducer>)producer didCompleteWithError:(NSError *)error;
@end
@protocol AsyncProducer
// consumer is retained until completion or cancellation.
// return value is cancellation token. when return value is deallocated,
// the producer is cancelled. if producer already completed no further action is taken on deallocation.
- (id<NSObject>)startWithConsumer:(id<AsyncConsumer>)consumer;
@end
@protocol Cancellable <NSObject>
- (void)cancel;
@end
// an utility object which can be returned from implementations of -[id<AsyncProducer> startWithConsumer:].
// this is nice because consumers don't have to explicitly call cancel on anything, just retain/release
// the object returned from startWithConsumer
@interface Cancellation : NSObject {
id<Cancellable> cancellable;
}
+ (id)cancellationWithCancellable:(id<Cancellable>)cancellable;
@end
@interface NSObject (AsyncConsumer)
// receiver is target. selector takes one argument: result or error
- (id<AsyncConsumer>)consumerWithResultSelector:(SEL)resultSelector;
- (id<AsyncConsumer>)consumerWithResultSelector:(SEL)resultSelector errorSelector:(SEL)errorSelector;
@end
@interface NSArray (ParallelProducers)
// all items in the array must be id<AsyncProducer>
- (id<AsyncProducer>)parallelProducer;
@end
@interface Async : NSObject
+ (void)fireAndForgetProducer:(id<AsyncProducer>)producer;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment