Some code instead of a long explanation.
The problem is :
When called from viewDidLoad, the server call triggers the delegates methods (connectionDidFinishLoading, etc…). But when called from XMLFileFullParsed, the request is launched to the server, but those same methods are not triggered.
If i call [self loadXMLDatas] instead of threading it, both server calls work fine.
Why ?
I start here :
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(foundaTag:) name:@"foundaTag" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(XMLFileFullParsed:) name:@"XMLFileFullParsed" object:nil];
self.server = [[ServerCommManager alloc] initWithServer:@"http://someserver/"];
[self.server sendQuestionWithCallIdentifier:@"IDENTIFIER" onPage:@"testpage.php" withParams:nil sendAnswerToObject:self]; // -- This one works
[NSThread detachNewThreadSelector:@selector(loadXMLDatas) toTarget:self withObject:nil];
}
- (void) loadXMLDatas {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Do things, create a XMLparser, start to parse.
[pool release];
}
- (void) XMLFileFullParsed:(NSNotification*)notification {
[self.server sendQuestionWithCallIdentifier:@"IDENTIFIER" onPage:@"testpage.php" withParams:nil sendAnswerToObject:self]; // -- This one doesn't work
}
XMLParser.m
- (void) doYourJob {
[self doTheJob];
[[NSNotificationCenter defaultCenter] postNotificationName:@"XMLFileFullParsed" object:nil userInfo:nil];
}
- (void) doTheJob:(XMLTag*)tag {
if ([tag.name isEqualToString:@"searchedtag"]) {
theData = extract some datas
[[NSNotificationCenter defaultCenter] postNotificationName:@"foundaTag" object:self userInfo:[NSDictionary dictionaryWithObject:theData forKey:@"data"]];
return;
}
for (XMLTag* aTag in tag.childtags) {
[self doTheJob:aTag];
}
}
Deep inside the server comm.m
- (void) executeRequest {
some init
self.connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
}
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
}
From the code you’ve given, the notification is being posted from another Thread rather than the Main Thread where you’ve registered the observer.
In Apple’s Notification Documentation, it says
What you can do is move the notification into it’s own method and then use performSelectorOnMainThread like so: