NSURLConnectionVCR / NSURLConnectionVCR / NSURLConnectionVCR.m

Diff from to

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;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.