Informing mainthread about finishing background thread execution.

Discussion in 'iPhone/iPad Programming' started by RookieAppler, Jan 24, 2013.

  1. RookieAppler, Jan 24, 2013
    Last edited by a moderator: Jan 24, 2013

    macrumors member

    Joined:
    Mar 15, 2012
    #1
    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
     
  2. macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #2
    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.
     
  3. thread starter macrumors member

    Joined:
    Mar 15, 2012
    #3
    @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.
     
  4. macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #4
    Are you starting and stopping the networkstatusindicator on the background thread? If so, you need to do this on the main thread.
     
  5. thread starter macrumors member

    Joined:
    Mar 15, 2012
    #5
    @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?
     
  6. macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #6
    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];
        });
    
     
  7. thread starter macrumors member

    Joined:
    Mar 15, 2012
    #7
    @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.
     
  8. macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #8

Share This Page