Skip to content

Commit

Permalink
Experimental delegate support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris committed Jan 10, 2012
1 parent d987091 commit 3b2e224
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion FSURLOperation.h
Expand Up @@ -16,7 +16,8 @@
@property (strong) NSError * error;
@property (strong) NSThread* targetThread; // changing this after the request has started produces undefined behavior.
@property (copy) void(^onFinish)(NSHTTPURLResponse* resp, NSData* payload, NSError* error);
// todo: delegate based callbacks
@property (weak) id delegate;
@property (assign) SEL callback;

+ (FSURLOperation*)URLOperationWithRequest:(NSURLRequest*)req
completionBlock:(void(^)(NSHTTPURLResponse* resp, NSData* payload, NSError* error))completion;
Expand Down
13 changes: 12 additions & 1 deletion FSURLOperation.m
Expand Up @@ -34,6 +34,8 @@ @implementation FSURLOperation
@synthesize error;
@synthesize targetThread;
@synthesize onFinish;
@synthesize delegate;
@synthesize callback;

@synthesize state;
@synthesize connection;
Expand Down Expand Up @@ -101,7 +103,16 @@ - (void)finish
[self willChangeValueForKey:@"isFinished"];
self.state = finished;
if (self.onFinish) self.onFinish(self.response, self.payload, self.error);
// TODO: Delegate-based callbacks
if (self.delegate&&self.callback) {
NSInvocation* inv = [[NSInvocation alloc] init];
[inv setTarget:self.delegate];
[inv setSelector:self.callback];
[inv setArgument:(__bridge void*)self.response atIndex:2];
[inv setArgument:(__bridge void*)self.payload atIndex:3];
[inv setArgument:(__bridge void*)self.error atIndex:4];

[inv invoke];
}
[self didChangeValueForKey:@"isFinished"];
}

Expand Down

0 comments on commit 3b2e224

Please sign in to comment.