views:

46

answers:

2

Hi I am trying to create a NSOperaion Queue to download a bunch of PDF files. But it doesnt work. The delegate methods dont get called for NSURLConnection since i put them in NSOperation queue.... any alternatives or solution???

- (void) loadData {
 NSOperationQueue *queue = [NSOperationQueue new];
 NSInvocationOperation *operation;
 for(int i=0;i<[self.pdfArray count];i++){
  NSString *url = [NSString stringWithFormat:@"http://www.somelink.com/%@.pdf",[self.pdfArray objectAtIndex:i]];
  operation = [[NSInvocationOperation alloc] initWithTarget:self 
               selector:@selector(loadDataWithOperation:) 
                 object:url];

  [queue addOperation:operation];
  [operation release];
 }
}

- (void) loadDataWithOperation:(NSString *) url{

 // Create the request.

    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
            cachePolicy:NSURLRequestUseProtocolCachePolicy
            timeoutInterval:60.0];

    NSURLConnection  *theDownload = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
}
A: 

I can't really see a problem with the code but it might be a threading issue. NSOperationQueue creates a thread through Grand Central Dispatch to run the operation. If NSURLConnection then also tries to create a thread it might cause an issue - I'm not sure a thread can be a child of a child thread.

You could do an sendSynchronousRequest: so that it stays in the thread that you have created in the NSOperationQueue and see if that works better.

Rudiger
A: 

take a look here, this is a tutorial that was helpful to me so I bookmarked it

http://www.dribin.org/dave/blog/archives/2009/05/05/concurrent_operations/

Aaron Saunders