Commits

Anders Carlsson  committed c33f2eb

Support initing with startImmediately:NO.

Don't send the response directly when initializing a connection with `initWithRequest:delegate:startImmediately:` with startImmediately set to NO, instead send the response when `start` is called.

  • Participants
  • Parent commits b1560d1

Comments (0)

Files changed (1)

File NSURLConnectionVCR/NSURLConnectionVCR.m

     return (origImps != NULL);
 }
 
+- (void)sendVCRCachedResultFor:(NSURLRequest *)request to:(id)realDelegate {
+  VCRCache* cache = [VCRCache loadCacheForRequest:request];
+
+  NSOperationQueue* queue = [NSOperationQueue currentQueue];
+  if (!queue) {
+    queue = [[NSOperationQueue alloc] init];
+  }
+  [queue addOperationWithBlock:^{
+    // yes, we want self and realDelegate to be retained by this block...
+    // it's perhaps a bit unnatural to call these delegate methods right after each other, but let's see if it is sufficient.
+    [realDelegate connection:(NSURLConnection*)self didReceiveResponse:cache.response];
+    NSUInteger loc = 0;
+    NSUInteger len = 256u * 1024u;
+    NSUInteger totalLen = [cache.responseBody length];
+    do {
+      if (len + loc > totalLen) {
+        len = totalLen - loc;
+      }
+      NSData* subData = [cache.responseBody subdataWithRange:NSMakeRange(loc, len)];
+      [realDelegate connection:(NSURLConnection*)self didReceiveData:subData];
+      loc += len;
+    } while (loc < totalLen);
+    [realDelegate connectionDidFinishLoading:(NSURLConnection*)self];
+  }];
+}
+
 #pragma mark Class posing / Swizzle bizz
 
 static const unsigned char swizzleCount = 3;
 
 - (id)initWithRequest:(NSURLRequest *)request delegate:(id)realDelegate startImmediately:(BOOL)startImmediately {
     VCRCache* cache = [VCRCache loadCacheForRequest:request];
-    if (cache) {
-        NSOperationQueue* queue = [NSOperationQueue currentQueue];
-        [queue addOperationWithBlock:^{
-            // yes, we want self and realDelegate to be retained by this block...
-            // it's perhaps a bit unnatural to call these delegate methods right after each other, but let's see if it is sufficient.
-            [realDelegate connection:(NSURLConnection*)self didReceiveResponse:cache.response];
-            NSUInteger loc = 0;
-            NSUInteger len = 256u * 1024u;
-            NSUInteger totalLen = [cache.responseBody length];
-            do {
-                if (len + loc > totalLen) {
-                    len = totalLen - loc;
-                }
-                NSData* subData = [cache.responseBody subdataWithRange:NSMakeRange(loc, len)];
-                [realDelegate connection:(NSURLConnection*)self didReceiveData:subData];
-                loc += len;
-            } while (loc < totalLen);
-            [realDelegate connectionDidFinishLoading:(NSURLConnection*)self];
-        }];
+    if (cache && startImmediately) {
+        [self sendVCRCachedResultFor:request to:realDelegate];
         return [self init];
     } else {
         // NSURLConnection retains its delegate until done loading --> VCRConnectionDelegate retains realDelegate, vcr, request until done loading
         delegate.realDelegate = realDelegate;
         delegate.request = request;
         delegate.vcr = self;
-        if ([connection initWithRequest:request delegate:delegate startImmediately:startImmediately]) {};  // wrapped in if() to silence `Expression result unused` compiler warning
+
+        if (!cache) {
+          if ([connection initWithRequest:request delegate:delegate startImmediately:startImmediately]) {};  // wrapped in if() to silence `Expression result unused` compiler warning
+        }
         return [self init];
     }
 }
     return [delegate realDelegate];
 }
 
+- (void)start {
+  [self sendVCRCachedResultFor:delegate.request to:delegate.realDelegate];
+}
+
 @end
 
 @protocol NSURLConnectionDataDelegate;