Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RookieAppler

macrumors member
Original poster
Mar 15, 2012
58
0
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.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
How long does this line take to execute?:
[self deletePreviousValues];
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.
 

RookieAppler

macrumors member
Original poster
Mar 15, 2012
58
0
@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

Dispatching
Dispatched
Getting customer values
Deletion done

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

malloc: *** error for object 0x9254e04: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
 
Last edited:

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
@dejo. That is an excellent observation. That method was the culprit. It took 13 seconds.
So i put it in another background like this

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.