TFHpple request not running on background thread?

150 Views Asked by At

this works fine on the main thread but for some reason doesn't work when running on a background thread. If you just comment out the call to dispatch_async than the program works fine!

Code:

NSString *requestString = [NSString stringWithFormat:@"http://www.songlyrics.com/index.php?section=search&searchW=%@+%@&submit=Search",actualSongName,actualSongArtist];
        NSLog(@"0");
        TFHpple *requestParser = [TFHpple hppleWithHTMLData:[NSData dataWithContentsOfURL:[NSURL URLWithString:requestString]]];
        NSLog(@"1");
        NSString *requestedURLXPathQuery = @"//a";
        NSArray *requestedStringsNodes = [requestParser searchWithXPathQuery:requestedURLXPathQuery];
        NSLog(@"2");

This is what gets logged:

2012-10-11 21:39:05.636 SuperViewer[6612:110b] 0
2012-10-11 21:39:05.651 SuperViewer[6612:907] Unable to parse.
2012-10-11 21:39:06.520 SuperViewer[6612:110b] 1
2012-10-11 21:39:06.565 SuperViewer[6612:110b] 2

This is all running on a background thread. Also I have logged the "requestString" var and its fine.

Any ideas what the problem is? Does Hpple not run on background threads?

Thanks!

1

There are 1 best solutions below

1
On

I don't know what's your exact issue. However Use NSThread instead of using dispatch_async. Make sure to use

  -[NSObject performSelectorOnMainThread:withObject:waitUntilDone:] 

to run on main thread (for Ui interactive functionality)