Using NSOperationQueue for downloading forms in background
<p>I need to download a number of forms from web service.For that I have used NSOperationQueue like this</p>

<pre><code>operationQueue = [NSOperationQueue new];
    for (int i=0;i&lt;[tempArray count];i++) {
        CheckList * checklist = (CheckList *)[tempArray objectAtIndex:i];

        NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                                                                                selector:@selector(downloadChecklistsInBackground:)
                                                                                  object:checklist];
        [operationQueue addOperation:operation];
</code></pre>

<p>The idea is , it should get executed without affecting the screen operations. And in  the UI there will be separate download button for each of the forms.So if user clicks on any one of them, it should be downloaded immediately and it should be removed from the background process.The code is given below.</p>

<pre><code>-(void)downloadChecklistsInBackground:(CheckList *)checklist
{
    BOOL isDone = NO;
    for (int j=0; j&lt;[selectedArray count]; j++) {
        if([checklist.checklistId isEqualToString:[selectedArray objectAtIndex:j]])
        {
            isDone = YES;

        }
    }
    if(!isDone)
    {
        [backGroundQueueArr addObject:checklist];

        NSString * urlStr = [[BASE_URL stringByAppendingString:DOWNLOAD_CHECKLIST_URL] stringByAppendingFormat:@"%@/%d/%@/%@/%@",[ChecklistSingleton sharedSingleton].userSSO,[checklist.checklistId intValue],checklist.language,checklist.baseFormId,checklist.versionNo];
        NSURL * url = [NSURL URLWithString:urlStr];
        NSLog(@"url is %@",url);

        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

        NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
        AppDelegate * appDelegateObj = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [request setValue:[NSString stringWithFormat:@"Bearer %@",appDelegateObj.accessToken ]forHTTPHeaderField:@"Authorization"];
        [request setTimeoutInterval:240.0];
        [request setValue:@"Application/JSON" forHTTPHeaderField:@"Content-Type"];
        NSURLSessionDataTask * downloadTask =[session dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error)
                                              {
                                                  NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                                  NSLog(@"response is %@",str);
                                              }];
        [downloadTask resume];

    }


}
-(void)downloadCecklistWithChecklist:(CheckList *)check5
{

    [selectedArray addObject:check5];
    BOOL isDownloaded = NO;
    for (int j=0; j&lt;[backGroundQueueArr count]; j++) {
        CheckList * checklistTobeChecked = [backGroundQueueArr objectAtIndex:j];
        if([checklistTobeChecked.checklistId isEqualToString:check5.checklistId])
        {
            isDownloaded = YES;
        }
    }
    if(!isDownloaded)
    {

    NSString * urlStr = [[BASE_URL stringByAppendingString:DOWNLOAD_CHECKLIST_METADATA_SUBURL] stringByAppendingFormat:@"%@/%d/%@/%@/%@",[ChecklistSingleton sharedSingleton].userSSO,[check5.checklistId intValue],check5.language,check5.baseFormId,check5.versionNo];
    NSURL * url = [NSURL URLWithString:urlStr];
    NSLog(@"url is %@",url);

    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

    NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
    AppDelegate * appDelegateObj = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [request setValue:[NSString stringWithFormat:@"Bearer %@",appDelegateObj.accessToken ]forHTTPHeaderField:@"Authorization"];
    [request setTimeoutInterval:240.0];
    [request setValue:@"Application/JSON" forHTTPHeaderField:@"Content-Type"];
    NSURLSessionDataTask * downloadTask =[session dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error)
                                          {
                                              NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                              NSLog(@"response is specific %@",str);
                                          }];
    [downloadTask resume];
    }


}
</code></pre>

<p>Could you please help me with the correct approach for this.I know that, this piece of code doesn't implement threading properly. Please guide me on this.Thank you very much in advance.</p>