Created
July 9, 2011 04:50
-
-
Save bvanderveen/1073329 to your computer and use it in GitHub Desktop.
Async workflows for Obj-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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