Note that there are some explanatory texts on larger screens.

plurals
  1. PONSUrlConnection in multiple NSOperation instances in a NSOperationQueue
    primarykey
    data
    text
    <p>The following code adds multiple NSOperation instances in an NSOperationQueue. The Operation just takes the contents of a url. I'm providing the php code too...</p> <p>Given the following code...</p> <pre><code>-(void)requestResponse { NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.someurl.gr/test.php"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:40.0]; NSDate *dd = [NSDate date]; NSURLResponse *resp; NSData *returnedData = [NSURLConnection sendSynchronousRequest:req returningResponse:&amp;resp error:NULL]; NSString *ss = [[[NSString alloc] initWithData:returnedData encoding:NSUTF8StringEncoding] autorelease]; NSLog(@"%@ - %.2f",ss , -[dd timeIntervalSinceNow]); } -(NSOperation*)task { NSInvocationOperation* theOp = [[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(requestResponse) object:nil] autorelease]; return theOp; } -(IBAction)buttonAction:(id)sender { NSOperationQueue *opq = [[NSOperationQueue alloc] init]; [opq setMaxConcurrentOperationCount:40]; for(int i=0; i&lt;15;i++) { [opq addOperation:[self task]]; [NSThread sleepForTimeInterval:1.0]; // here is the issue! } [opq release]; } </code></pre> <p>The contents of the test.php witch -requestResponse calls:</p> <pre><code>&lt;?php echo "Through!"; for($i=0;$i&lt;1000000;$i++) { // don't return too soon } ?&gt; </code></pre> <p>The issue is that when i use <code>[NSThread sleepForTimeInterval:1.0]</code> to create a delay before each NSOperation addition in the queue, all requests take about the same time to complete. If I comment this line most requests take considerably <strong>increasingly</strong> more and more time to complete. The question is why? </p> <p>I have tested the url from command line (using curl) and the requests take the same time to complete for any number of simultaneous calls to the php, so the issue is not server side.</p> <p>Here is the output using localhost as server <strong>with sleepForTimeInterval disabled</strong></p> <pre><code>[s] Through! - 0.22 [s] Through! - 0.23 [s] Through! - 0.25 [s] Through! - 0.26 [s] Through! - 0.26 [s] Through! - 0.28 [s] Through! - 0.43 [s] Through! - 0.46 [s] Through! - 0.49 [s] Through! - 0.50 [s] Through! - 0.52 [s] Through! - 0.51 [s] Through! - 0.60 [s] Through! - 0.62 [s] Through! - 0.63 </code></pre> <p>// of course the differences is much more higher (from 7 to 20 secs!) with a php doing some real work. </p> <p>and with <strong>sleepForTimeInterval enabled</strong></p> <pre><code>[s] Through! - 0.23 [s] Through! - 0.09 [s] Through! - 0.09 [s] Through! - 0.09 [s] Through! - 0.09 [s] Through! - 0.09 [s] Through! - 0.08 [s] Through! - 0.09 [s] Through! - 0.09 [s] Through! - 0.08 [s] Through! - 0.09 [s] Through! - 0.09 [s] Through! - 0.09 [s] Through! - 0.09 [s] Through! - 0.09 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload