Quote:
Originally Posted by shivangvyas
Connection delegate methods not called with background thread, Why ?
In program i have created background thread and that thread set connection delegates, by setting ObjNSURLConnection.delegate = AnotherClass, and that AnotherClass contains NSURLConnection delegate methods.
But when i debug a code i have found that NSURLConnection delegate method doesn't called. Any reason , Why they are not called ? How to solve this issue?
|
I've found myself in the same problem yesterday.
This happens because the thread that created the NSURLConnection no longer exists when it tries to call back its delegate.
My solution was to keep the thread alive until the connection is done:
Code:
-(void)main{
[self startUpdate]; //This method initializes the NSURLConnection
NSRunLoop *theRL = [NSRunLoop currentRunLoop];
while (!finished && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]){
//Keep alive the Thread
}
}
I created an instance variable ("finished") that works as a flag. When the connection did finish to download or fail, it sets the finished flag in true. To achieve this, I worked with the NSNotificationCenter.
I don't know if this is the best solution, but it worked great for me.
Also, I'm using NSOperation and NSOperationQueue here.