Permalink
Browse files

Experimental delegate support.

  • Loading branch information...
1 parent d987091 commit 3b2e224aaf9d618a4f8d4f8d75115613f4ce7727 Chris committed Jan 10, 2012
Showing with 14 additions and 2 deletions.
  1. +2 −1 FSURLOperation.h
  2. +12 −1 FSURLOperation.m
View
@@ -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;
View
@@ -34,6 +34,8 @@ @implementation FSURLOperation
@synthesize error;
@synthesize targetThread;
@synthesize onFinish;
+@synthesize delegate;
+@synthesize callback;
@synthesize state;
@synthesize connection;
@@ -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"];
}

0 comments on commit 3b2e224

Please sign in to comment.