Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 24, 2013, 11:33 AM   #1
RookieAppler
macrumors member
 
Join Date: Mar 2012
Informing mainthread about finishing background thread execution.

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 dejo; Jan 24, 2013 at 11:59 AM. Reason: Please use [code] tags.
RookieAppler is offline   0 Reply With Quote
Old Jan 24, 2013, 12:31 PM   #2
KoolStar
macrumors Demi-God
 
KoolStar's Avatar
 
Join Date: Oct 2006
Location: Kentucky
Send a message via AIM to KoolStar Send a message via Skype™ to KoolStar
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.
__________________
"Innovation distinguishes between a leader and a follower." : S.J.
KoolStar is offline   0 Reply With Quote
Old Jan 24, 2013, 01:49 PM   #3
RookieAppler
Thread Starter
macrumors member
 
Join Date: Mar 2012
@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.
RookieAppler is offline   0 Reply With Quote
Old Jan 24, 2013, 01:51 PM   #4
KoolStar
macrumors Demi-God
 
KoolStar's Avatar
 
Join Date: Oct 2006
Location: Kentucky
Send a message via AIM to KoolStar Send a message via Skype™ to KoolStar
Are you starting and stopping the networkstatusindicator on the background thread? If so, you need to do this on the main thread.
__________________
"Innovation distinguishes between a leader and a follower." : S.J.
KoolStar is offline   0 Reply With Quote
Old Jan 24, 2013, 02:00 PM   #5
RookieAppler
Thread Starter
macrumors member
 
Join Date: Mar 2012
@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?
RookieAppler is offline   0 Reply With Quote
Old Jan 24, 2013, 02:03 PM   #6
KoolStar
macrumors Demi-God
 
KoolStar's Avatar
 
Join Date: Oct 2006
Location: Kentucky
Send a message via AIM to KoolStar Send a message via Skype™ to KoolStar
Quote:
Originally Posted by RookieAppler View Post
@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];
    });
__________________
"Innovation distinguishes between a leader and a follower." : S.J.
KoolStar is offline   0 Reply With Quote
Old Jan 24, 2013, 02:24 PM   #7
RookieAppler
Thread Starter
macrumors member
 
Join Date: Mar 2012
@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.
RookieAppler is offline   0 Reply With Quote
Old Jan 24, 2013, 02:27 PM   #8
KoolStar
macrumors Demi-God
 
KoolStar's Avatar
 
Join Date: Oct 2006
Location: Kentucky
Send a message via AIM to KoolStar Send a message via Skype™ to KoolStar
With out knowing the exact working of your code I'm not 100 percent sure.

I would take a look at: http://www.cocoanetics.com/2010/05/n...round-threads/
__________________
"Innovation distinguishes between a leader and a follower." : S.J.
KoolStar is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Tags
background, indicator, ios, notification, thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 02:33 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC