Reputation: 2299
I'm facing a problem in a simple TableView : i want to refresh the data from the Net in a separate thread to avoid blocking the UI. I've created a NSoperation that will do the job. This object creates an other object to handle the download. The delegate of the downloader is the NSoperation Object.
My problem is that my NSoperation Object does not get any call back from the Downloader.
If I don't you a separate thread, using the same code (and calling main method), it works perfectly.
Upvotes: 0
Views: 813
Reputation: 982
As Mirage said, the NSURLConnection is designed to use a runloop. You get a running runloop setup for you on the main thread, but in other threads you create the runloop has to be managed manually (basically you have to run it).
My advice would be to use a concurrent NSOperation and in the start method check if you are on the main thread: if not, call again the start method on the main thread and bail out
-(void)start
{
if ([NSThread mainThread] != [NSThread currentThread]) {
[self performSelectorOnMainThread:@selector(start)];
return;
}
rest of the real code is here ....
}
The other options (cleaner but definitely harder) is to create an NSOperation that manages an internal runloop (or one supplied by the caller), so that async APIs that need it (like NSURLConnection) can be run on threads different than main.
Search for "LinkedImageFetcher" sample in the official api: the class QRunLoopOperation does exactly that.
Upvotes: 1
Reputation: 3973
Example code would be nice. Is there any chance you use NSURLRequest in that separate thread? If so, try spawning the NSURLRequest on the main thread (NSURLRequest can be async by itself anyway, so that is perfectly fine).
EDIT I meant NSURLConnection, not request.
Upvotes: 0