2010-05-24 175 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 
    } 
}]; 

此操作在后台线程中处理