Ganapathy
Ganapathy

Reputation: 4614

Main Thread processing issue

In my application i am using back ground thread for hitting multiple service and perform operation with core data. I have used main thread for back ground process ,Its working fine. Here is my code

    dispatch_queue_t main = dispatch_get_main_queue();
    dispatch_async(main, 
                   ^{ 
                       [self backGroundCall]; 
                   }); 

-(void)backGroundCall
{
   NSLog(@"Done");
        if([CacheManager refreshDBforFirstTimeUseWithDelegate:self])
       {
           [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"IsDBInitialized"];

            ContainerViewController *containerViewControllerInstance = [ContainerViewController getContainerInstance];
            [containerViewControllerInstance setUserId:_userID];

            [progressView setHidden:YES];
            [self.view setUserInteractionEnabled:YES];

            [self.navigationController setDelegate:containerViewControllerInstance];
            [self.navigationController pushViewController:containerViewControllerInstance animated:YES];
        }

}

once i initialize the data base , i need to navigate to the container view.During the initialization i will display one progress bar. That is working fine, when the entire background process is completed(app is in minimized state). During the background process if i come to the foreground progress bar is not showing at that time black screen is display instead of progress view . After the completion of the main threat container view all not display[if i comes to foreground of main thread process].

enter image description here

i need to show the progress bar, if i come back to the app in the middle of the main thread process. Please guide me to fix this issue.

Thanks.

Upvotes: 1

Views: 304

Answers (1)

Rui Peres
Rui Peres

Reputation: 25927

   dispatch_queue_t main = dispatch_get_main_queue();
    dispatch_async(main, 
                   ^{ 
                       [self backGroundCall]; 
                   }); 

This is a bit misleading... You call the method backGroundCall, but you are actually doing this on the main thread. If you want to make some operation on a working thread, you can do this:

  // Declare the queue
         dispatch_queue_t workingQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
         dispatch_async(workingQueue,
                        ^{
                          // My background job
                            dispatch_async(dispatch_get_main_queue(),
                                           ^{
                                              // Update the UI
                                           }
                                           );
                        });

Upvotes: 4

Related Questions