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 pulls data from web service in XML. It is quite large data. So i am doing that is background thread like this
Code:
	dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
													 (unsigned long)NULL), ^(void) {
				[self getAllCustomerValues];
			});
- (void)getAllCustomerValues
{
....do work
//at the end of this method
	NSNotification *notification =[NSNotification notificationWithName:@"reloadRequest"
																object:self];
	[[NSNotificationCenter defaultCenter] postNotification : notification];
}
I post a notification.This is screen A.
In my other screen, screen B I listen for notification to act on it.
Code:
//Screen B
-(void)viewDidLoad
{
...some work
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(stopActivityIndicator)
												 name:@"reloadRequest"
											   object:nil];
}
-(void)stopActivityIndicator
{
..do some work
}
My question is since i am notifying on background thread how is it possible for the main thread to know about a notification. I want to tell main thread about the background thread finishing its work and then do what is described in the notification method. If you have questions, please ask. Thanks
 
Last edited by a moderator:

KoolStar

macrumors demi-god
Oct 16, 2006
825
9
Kentucky
My first question is does this work as of now with the code you have?

If so then your simple question of how does the main thread know what the notification is, thats simple the notificationcenter is a singleton based class that can forward notifications to any subscribing classes.

If it does not work. I would check how you are actually posting the notification.
 

RookieAppler

macrumors member
Original poster
Mar 15, 2012
58
0
@KoolStar. It some how started working. But i log while the parsing is going on (background thread is running).I see values getting logged. It finishes the parsing and still the indicator keeps spinning. Indicator stops and hides like 30 sec after the parsing has finished. Its not immediate . Am i clear? Please ask me questions if its not clear.More questions coming.Thanks.
 

KoolStar

macrumors demi-god
Oct 16, 2006
825
9
Kentucky
Are you starting and stopping the networkstatusindicator on the background thread? If so, you need to do this on the main thread.
 

RookieAppler

macrumors member
Original poster
Mar 15, 2012
58
0
@koolstar. Starting no. I start it on mainthread. When the background thread is finished i launch the method to stop the activity indicator . I send a notification basically. In the screen 2 i observe the notification and stop and hide the activity indicator when i hear from it. So can i say that stopping indicator is performed by background thread?
 

KoolStar

macrumors demi-god
Oct 16, 2006
825
9
Kentucky
@koolstar. Starting no. I start it on mainthread. When the background thread is finished i launch the method to stop the activity indicator . I send a notification basically. In the screen 2 i observe the notification and stop and hide the activity indicator when i hear from it. So can i say that stopping indicator is performed by background thread?

Thats the issue notifications are sent on the same thread. So therefore your notification is being received on the background thread.

You will need to:
Code:
    dispatch_async(dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    });
 

RookieAppler

macrumors member
Original poster
Mar 15, 2012
58
0
@Koolstar. I tried placing your exact code in Screen 1 at the very end of my method (getAllCustomerValues) where i parse the XML values. Did not work. Then i placed it at the end of viewDidLoad of screen 2. It did not work.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.