开发者

ASIHTTPRequest causes crash after interrupting Internet connection

开发者 https://www.devze.com 2023-03-19 00:56 出处:网络
I\'m using ASIHTTPRequest to download data from the internet. If I lose my internet connection, and I try to load the data the app crashes.

I'm using ASIHTTPRequest to download data from the internet. If I lose my internet connection, and I try to load the data the app crashes.

Code:

- (IBAction)load:(id)sender { 
    if ([act isAnimating]) {
        NSLog(@"Request already being processed!");
    }
    else {
    ASINetworkQueue *queue = [ASINetworkQueue queue];
    ASIHTTPRequest *usdRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADUSD=X&f=l1"]];
    ASIHTTPRequest *eurRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=l1"]];
    ASIHTTPRequest *dateRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=d1"]];
    ASIHTTPRequest *timeRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=t1"]];

    [queue addOperation:usdRequest];
    [queue addOperation:eurRequest];
    [queue addOperation:dateRequest];
    [queue addOperation:timeRequest];

    [queue setQueueDidFinishSelector:@selector(parseLoadedData:)];
    [queue setRequestDidFinishSelector:@selector(requestLoaded:)];
    [queue setDelegate:self];
    [queue go];
    [act startAnimating];
    }
}

- (void)requestLoaded:(ASIHTTPRequest *)request {
     if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADUSD=X&f=l1"]]) {
        usdString = [[request responseString] retain];
    } else if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=l1"]]) {
        eurString = [[request responseString] retain];
    } else if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=开发者_开发技巧d1"]]) {
        dateString = [[request responseString] retain];
    } else if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=t1"]]) {
        timeString = [[request responseString] retain];

}
}

- (void)parseLoadedData:(ASIHTTPRequest *)request {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *Date = [dateString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    NSString *Time = [timeString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    NSString *total = [NSString stringWithFormat:@"%@ %@",Date,Time];

    if ([eurString length] == 0) {
        test.text = [defaults objectForKey:@"date"];
        eur.text = [defaults objectForKey:@"eur"];
        usd.text = [defaults objectForKey:@"usd"];

    } else {
        test.text = total;
        eur.text = eurString;
        usd.text = usdString;
        [defaults setObject:test.text forKey:@"date"];
        [defaults setObject:usd.text forKey:@"usd"];
        [defaults setObject:eur.text forKey:@"eur"];
    }

    [defaults synchronize];
    [eurString release];
    [usdString release];
    [dateString release];
    [timeString release];
    [act stopAnimating];

}


Ok since nobody answered, i looked more in to it, and I found out, that when the request is complete, i had to set the text for each downloaded string to @"", so that when the next request is done, and there is no internet connection the string stays @"" so that in pareLoadData the app recognized the [eurString length] == 0. So basically i just needed to add

    eurString = nil;
    usdString = nil;
    gbpString = nil;
    jpyString = nil;
    audString = nil;

after

    [eurString release];
    [usdString release];
    [dateString release];
    [timeString release];

i hope this helps if anybody else has a similar problem. Im very happy now, now my app is super stable and cant crash at all! :)

0

精彩评论

暂无评论...
验证码 换一张
取 消