multithreading - iphone ios thread issue: delegate object not responding when using dispatch_async -
here's 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 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 necessary nsxmlparserdelegate protocol methods implemented - not shown here*/
problem: none of nsxmlparserdelegate protocol methods fired. not know why. if remove "dispatch_async" in didfinishlaunchingwithoptions method, things working expected - nsxmlparserdelegate protocol methods fired.
anyone see problem?
two things jump out:
nsurlconnection requires run loop operate. when run on main thread, there's automatically run loop connection schedule on. when use dispatch_async(), block scheduled in secondary thread may not have run loop, or run loop may not in mode allows nsurlconnection operate.
even if connection schedule itself, block ends right after connection created. nsurlconnection expects send delegate messages on thread created, how work if block has ended? there sort of valid context in delegate can called on thread?
Comments
Post a Comment