2011-11-18 115 views
0

當我連接到測試timout的端口號時,我的應用程序凍結。 我打電話給[request setTimeoutInterval:10];,我認爲這應該是10秒鐘。但是,該應用程序掛起。它可能與它是一個本地服務器有關嗎?iPhone [請求setTimeoutInterval:10]不工作,應用程序凍結

代碼:

// call this when program first starts 
-(void) nSendMessage : (NSString *) name Password: (NSString *) password page: (NSString *) page 
{ 

    // set the url 
    NSString *address = @"http://localhost:1075/update"; 
    address=[ address stringByAppendingString: page]; 

    NSMutableURLRequest *request = 
    [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:address]]; 

    // post or get 
    [request setHTTPMethod:@"POST"]; 

    // data to send 
    NSString *postString = @"username=iusername&password=ipassword"; 
    NSString *sendString=[postString stringByReplacingOccurrencesOfString:@"iusername" withString: name]; 
    sendString=[sendString stringByReplacingOccurrencesOfString:@"ipassword" withString: password]; 

    [request setValue:[NSString 
         stringWithFormat:@"%d", [sendString length]] 
    forHTTPHeaderField:@"Content-length"]; 

    [request setHTTPBody:[sendString 
          dataUsingEncoding:NSUTF8StringEncoding]]; 

    [request setTimeoutInterval:10]; 
    [[NSURLConnection alloc] 
    initWithRequest:request delegate:self]; 

// 
//THE PROGRAM FREEZES HERE 
// 

    NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

    NSString *response = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding]; 

    // Phrase repl 
    [self nUpdateDisplay:response]; 

} 

回答

0

你的問題是這樣的一個愚弄的人:

iPhone SDK: URL request not timing out

所以總結:超時下限至少爲4分鐘,即使你通過以較小的價值。

而且如上述鏈接所詳述的,使用異步方法通常是最佳選擇。如果你使用同步,就像你正在做的那樣,並且你在主循環中,你將完全阻止你的用戶界面,這將導致'掛起'的效果。