iOS: App gets SIGKILL due to slow network connection?

随声附和 提交于 2019-12-11 18:19:16

问题


When I was at a hotel, their Wifi apparently was connected to the Internet via a very very slow Internet connection. It may have been modem based in fact.

The result was that my app's HTTP GET request appears to have caused iOS to send my app a SIGKILL (as Xcode indicates).

Why? How to fix?

Thanks.


回答1:


You need to put your HTTP request in a background thread. If your main thread is unresponsive for too long, your app will be terminated.

Normally, the API for web services provide an asynchronous fetch. You should be using that.

If your API does not provide such... use a different API. Barring that, put it in the background yourself. Something like

- (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);
}


来源:https://stackoverflow.com/questions/10288133/ios-app-gets-sigkill-due-to-slow-network-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!