Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone ios thread issue: delegate object not responding when using dispatch_async

Here's my code:

MainDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
FetchManager *fetchManager = [FetchManager sharedInstance];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^{
[fetchManager fetchData];
});

//regular code continues

}

FetchData.m(this is a singleton class)

- (id)getInstance { ... }

- (void)fetchData
{
    NSURL *url = [NSURL URLWithString:@"http://www.data.xml"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];

    if (connectionInProgress) {
        [connectionInProgress cancel];
        [connectionInProgress release];
    }

    [xmlData release];
    xmlData = [[NSMutableData alloc] init];

    connectionInProgress = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];  
}

/*and all necessary NSXMLParserDelegate protocol methods are implemented - not shown here*/

Problem: none of the NSXMLParserDelegate protocol methods are fired. I do not know why. If I remove the "dispatch_async" in the didFinishLaunchingWithOptions method, then things are working as expected - the NSXMLParserDelegate protocol methods are fired.

Anyone see the problem?

like image 647
Van Du Tran Avatar asked Jun 18 '26 00:06

Van Du Tran


1 Answers

Two things jump out:

  1. NSURLConnection requires a run loop to operate. When you run on the main thread, there's automatically a run loop that the connection will schedule itself on. When you use dispatch_async(), your block is scheduled in some secondary thread that may not have a run loop, or whose run loop may not be in a mode that allows NSURLConnection to operate.

  2. Even if the connection could schedule itself, the block ends right after the connection is created. NSURLConnection expects send its delegate messages on the thread where it was created, but how would that work if the block has ended? Is there some sort of valid context in which the delegate can be called on that thread?

like image 120
Caleb Avatar answered Jun 19 '26 15:06

Caleb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!