2010-05-24 86 views
0

我正在創建iphone應用程序。 因此,當應用程序啓動時,它會連接到服務器並下載少量圖像,然後繼續使用應用程序。當應用程序正在下載時,它將顯示初始啓動畫面。只要我的服務器能夠ping我的iphone,它的工作就很好。 但是當我的服務器花費很多時間來響應NSURL請求時,麻煩就開始了。 該應用程序與以下錯誤而崩潰:當應用程序連接超時時,iphone應用程序崩潰

Mon May 14 13:56:34 unknown Springboard[24] <Warning>: com.xxxx.xxx failed to launch in time 

我瞭解,當這些問題與應用程序發生,iphone崩潰appliation。我想知道iphone允許應用程序響應這種情況的最大時間。

是否有任何最大值?

回答

1

計時器有點像20-30秒,但這並不重要。

您正在同步下載數據。請使用NSURLConnection將程序更改爲異步下載。你的應用看起來要快得多,不會有終止的風險。您也可以實現超時的錯誤處理。

0

從的Readme.txt上Apple's Reachability example

The Reachability sample demonstrates the asynchronous use of the SCNetworkReachability API. You can use the API synchronously, but do not issue a synchronous check by hostName on the main thread. If the device cannot reach a DNS server or is on a slow network, a synchronous call to the SCNetworkReachabilityGetFlags function can block for up to 30 seconds trying to resolve the hostName. If this happens on the main thread, the application watchdog will kill the application after 20 seconds of inactivity.

正如保羅說,這是一個非常,非常糟糕的主意,做任何類型的同步聯網。你需要在iPhone上異步加載。

0

如果您在主線程,UI和主線程中處理的請求和響應操作將被阻止,並且可能需要一些時間才能接收響應。如果主線程在特定時間阻塞,WATCH DOG將退出您的應用程序。

更好的解決方案是在後臺線程或其他線程中運行您的請求。

例如

if(!backgroundQueue) 
    backgroundQueue=[[NSOperationQueue alloc]init]; 

NSURLRequest *request=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:url] 
              cachePolicy:NSURLCacheStorageNotAllowed 
             timeoutInterval:60]; 

[NSURLConnection sendAsynchronousRequest:request 
            queue:backgroundQueue 
         completionHandler:^(NSURLResponse *response,NSData *data,NSError *error) { 
    if (complete) { 
      // handle your logic here 
    } 
}]; 

此操作在後臺線程中處理