Search code examples
iosxcodesendasync

sending many AsynchronousRequests gives an error


I'm doing some kind of finding my device using this code:

NSOperationQueue* quququ;

int i=0;
for (i=0; i<256; i++) {
    //NSLog(@"%i",i);

    NSString *url=[NSString stringWithFormat:@"http://%i.%i.%i.%i/",myip1,myip2,myip3,i];
    //NSLog(@"urlGET = %@", url);

    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1];

    [NSURLConnection sendAsynchronousRequest:request queue:quququ completionHandler:^(NSURLResponse *respons, NSData *data, NSError *error) { 

    //do smth
    NSLog(@"data: %ld bytes. res: %@, error: %@", (long)[data length], respons, error);

}];

what am i doing wrong? i receive the "exc_bad_access" error.


Solution

  • In the first line of your code, you just declare an NSOperationQueue reference, but did not create a NSOperationQueue object, that is the root cause of "EXC_BAD_ACCESS" error. You should assign queue with a NSOperationQueue object and then use it.

    Change the first line of your code to

    NSOperationQueue* quququ = [[[NSOperationQueue alloc] init] autorelease];
    

    This will solve your problem.