0

当我在酒店时,他们的 Wifi 显然是通过非常慢的互联网连接连接到互联网的。事实上,它可能是基于调制解调器的。

结果是我的应用程序的 HTTP GET 请求似乎导致 iOS 向我的应用程序发送 SIGKILL(如 Xcode 所示)。

为什么?怎么修?

谢谢。

4

1 回答 1

1

您需要将 HTTP 请求放在后台线程中。如果您的主线程长时间无响应,您的应用程序将被终止。

通常,Web 服务的 API 提供异步获取。你应该使用那个。

如果您的 API 不提供此类...请使用不同的 API。除此之外,你自己把它放在后台。就像是

- (void)issuePotentiallyLongRequest
{
    dispatch_queue_t q = dispatch_queue_create("my background q", 0);
    dispatch_async(q, ^{
        // The call to dispatch_async returns immediately to the calling thread.
        // The code in this block right here will run in a different thread.
        // Do whatever stuff you need to do that takes a long time...
        // Issue your http get request or whatever.
        [self.httpClient goFetchStuffFromTheInternet];

        // Now, that code has run, and is done.  You need to do something with the
        // results, probably on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            // Do whatever you want with the result.  This block is
            // now running in the main thread - you have access to all
            // the UI elements...
            // Do whatever you want with the results of the fetch.
            [self.myView showTheCoolStuffIDownloadedFromTheInternet];
        });
    });
    dispatch_release(q);
}
于 2012-04-23T23:20:04.580 回答