ASIHTTPRequest在中断Internet连接后导致崩溃

时间:2011-07-08 09:58:31

标签: objective-c ios xcode crash asihttprequest

我正在使用ASIHTTPRequest从互联网上下载数据。如果我丢失了我的互联网连接,并且我尝试加载数据应用程序崩溃。

代码:

- (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];

}

1 个答案:

答案 0 :(得分:1)

好了,因为没有人回答,我更加注重,我发现,当请求完成后,我必须将每个下载字符串的文本设置为@"",以便当下一个请求完成,并且没有互联网连接时,字符串保持@""以便在pareLoadData中应用识别[eurString length] == 0。所以基本上我只需要添加

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

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

我希望如果其他人有类似的问题,这会有所帮助。我现在非常高兴,现在我的应用程序非常稳定,根本无法崩溃! :)