2011-07-08 47 views
0

我正在使用ASIHTTPRequest從Internet下載數據。如果我失去了互聯網連接,並且我嘗試加載應用程序崩潰的數據。ASIHTTPRequest在中斷Internet連接後導致崩潰

代碼:

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

好了,因爲沒有人回答,我看了更多的它,我發現,該請求完成時,我不得不爲每個下載的字符串文本設置爲@」 「,這樣當下一個請求完成,並且沒有互聯網連接時,字符串保持@」「,以便在pareLoadData該應用識別[eurString length] == 0。所以基本上我只需要後

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

我希望這會幫助,如果任何人有類似的問題,以增加

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

。我現在非常高興,現在我的應用程序超級穩定,不能崩潰! :)