Problem in NSURLConnection didFailWithError: Callback sent multiple times when Connection Timeout
I am using NSURLConnection to fetch data from an RSS Feed.I have set default time out value to 30 seconds.In Low wifi connectivity situations i get a timeout response(From did fail with error) as normal after 30 seconds.I make sure the connection is released when Callback comes.But after another 30 seconds(60seconds in total after request is sent) i get another Callback.I have tried to debug but the 2nd callback is not tracked any where.
Also this appears for the very first time when i send the request.Next time when i send the request only one call back appears. There is another thread with the same issue but i have not found any concrete answer there.I will really appreciate if someone can help me out.
Here is my connection creation,delegate,FailwithError and Callback Function
//MainClass.m
- (void)LoadRSSFeed {
if (Stories == nil) {
[activityIndicator startAnimating];
rssParser = [[Parser alloc] init];
[rssParser parseRssFeed:@"http://twitter.com/statuses/user_timeline/56202272.rss" withDelegate:self];
} else {
[newsTable reloadData];
}
}
//Parser Class where connection is called
- (void)parseRssFeed:(NSString *)url withDelegate:(id)aDelegate {
[self setDelegate:aDelegate];
ERROR = FALSE;
responseData = [[NSMutableData data] retain];
NSURL *baseURL = [[NSURL URLWithString:url] retain];
NSLog(@"Creating Connection");
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
conn = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Error");
ERROR = TRUE;
if (_delegate)
{
[_delegate receivedItems:nil];
}
else
{
}
}
//Delegate Callback Function in MainClass.m
- (void)receivedItems:(NSArray *)theItems {
[rssParser setDelegate:nil];//setting delegates to nil
[rssParser release];
rssParser = nil;
Stories = theItems;
if([activityIndicator isAnimating]){
if(Stories!=nil)
{
if([Stories count])
开发者_如何学Go {
[newsTable reloadData];
}
}else{
NSLog(@"Showing Alert");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Time Out" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil, nil];
[alert show];
[alert release];
}
}
[activityIndicator stopAnimating];
}
The alert View in callback function is being called twice.Once after normal 30 second timeout and one after 60 seconds which i am unable to track.Even if i add Breakpoints or NSlogs before the Alert view they are not called or captures.
Any help here would really be appreciated .
Azeem
The documentation for –[NSURLConnectionDelegate connection:didFailWithError:]
states the following:
Once the delegate receives this message, it will receive no further messages for
connection
.
Since the alert view is shown in your Parser
's delegate implementation, you might be calling receivedItems:
from other places than your Parser
object. In fact, since break points and log statements are ignored, you could even have some other code that shows the timeout alert (maybe a timer or something?) rather than a Parser
delegate callback.
精彩评论