Alert view blocked by background thread and preventing it to show on screen

Discussion in 'iPhone/iPad Programming' started by RookieAppler, Feb 21, 2013.

  1. macrumors member

    Joined:
    Mar 15, 2012
    #1
    I have a method that gets launched by a UIBarbuttonitem click. In that the first step is launch a alert view. Then a background thread starts to pull data from web service. I made sure that alert view is shown on main thread by using performselectoronmainthread. But still there is a 13 second delay from when i click the barbutton click and showing of alert view. Here is the code.


    Code:
    - (void)refreshDataAction
    {
     
    	//Put up an alert box indicating user to wait while data is loading.
    	
    	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Data is Loading"
    													message:@"Please wait while data is being refreshed."
    												   delegate:self
    										  cancelButtonTitle:@"OK"
    										  otherButtonTitles:nil, nil];
     
    	[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:FALSE];
    	
    	NSLog(@"Refresh Data");
    	self.txtCustomerSearch.text =@"";
    	[self cleanUPPreviousLabels];
    
    	
    	NSLog(@"hit in willpresenet alertview at 221");
    	self.refreshActIndicator.hidden = NO;
    	[self.refreshActIndicator startAnimating];
    	NSLog(@"Dispatching");
    
    	//Disable the view and all the other controls
    	self.txtCustomerSearch.userInteractionEnabled =NO;
    	self.txtCustomerSearch.enabled =NO;
    	self.btnSearch.enabled =NO;
    	self.btnSearch.userInteractionEnabled = NO;
    	self.scrollView.userInteractionEnabled = NO;
    	//self.view.userInteractionEnabled =NO;
    	self.chkButton.enabled = NO;
    	
    	
    	[self deletePreviousValues];
    	dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    		NSLog(@"Getting customer values");
    		[self getAllCustomerValues];
    		NSLog(@"Got customer values");
    		
    		NSError *nwerror = nil;
    		if (![self.secondMOC save:&nwerror])
    		{
    			NSLog(@"209 Failed to save second MOC");
    		}
    		else
    		{
    			//NSLog(@"saved success");
    		}
    		
    		
    		self.txtCustomerSearch.userInteractionEnabled = YES;
    		self.txtCustomerSearch.enabled =YES;
    		self.btnSearch.enabled =YES;
    		self.btnSearch.userInteractionEnabled = YES;
    		self.scrollView.userInteractionEnabled = YES;
    		self.view.userInteractionEnabled =YES;
    		self.chkButton.enabled = YES;
    		
    		[self.refreshActIndicator stopAnimating];
    	 
    		NSLog(@"Saved");
    		
    		
    	});
    	NSLog(@"Dispatched");	
    }
    Why is alert view not showing up immediately. OR Why is my UIThread( i am assuming my main thread) not showing the alertview immediately. Any help would be great. I am trying to get to solve this past 5 hours. So please help. It is in iOS 6.0 and iPad in-house project. If you need more information, please ask. Thanks.
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    How long does this line take to execute?:
    Because it's outside your background thread.

    P.S. You don't need to use performSelectorOnMainThread: if refreshDataAction is already running on the main thread.
     
  3. RookieAppler, Feb 21, 2013
    Last edited: Feb 21, 2013

    thread starter macrumors member

    Joined:
    Mar 15, 2012
    #3
    @dejo. That is an excellent observation. That method was the culprit. It took 13 seconds.
    So i put it in another background like this

    Code:
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    			[self deletePreviousValues];
    		NSLog(@"Deletion done");
    	});
    ...and other code continues like this
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    		NSLog(@"Getting customer values");
                    [self getAllCustomerValues];
    		NSLog(@"Got customer values");
    .....
    I want to be absolutely sure that deletePreviousValues is completed first and then only getAllCustomerValues gets called. But my simulator output is like this

    So i want "Deletion done" to be done before "Getting customer values" starts. I gave HIGH priority but still it is not getting done. Also sometimes it exceptions. What is the correct way?

    The exception is like this

     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Why put it in another background thread? Why not put it in the existing background thread? You want deletePreviousValues to be completed before calling getAllCustomerValues, then call them sequentially.
     

Share This Page