简体   繁体   English

在后台状态冻结 http 请求

[英]Freezing http requests in background state

There app have many tasks when app in background state, basically work with bluetooth dongles, and receive/sending data to server.当应用程序处于后台状态时,应用程序有很多任务,基本上与蓝牙加密狗一起工作,以及接收/发送数据到服务器。 Sometimes when app in background state send a query, system is "freeze in" this query.有时,当处于后台状态的应用程序发送查询时,系统会“冻结”此查询。 And after launch app, system is "freeze out", and we have启动应用程序后,系统“冻结”,我们有

Error Domain=kCFErrorDomainCFNetwork Code=-1001 "The request timed out.

App use last AFNetworking 3.1.0 at this moment, and BFTask helper.应用程序此时使用最后一个 AFNetworking 3.1.0,以及 BFTask 助手。

For more detail.欲知更多详情。 Imagine, user use app for some reason, after app goes app to background state (user press home button or lock device), app normally continue working at background state.想象一下,用户出于某种原因使用应用程序,应用程序进入后台状态(用户按下主页按钮或锁定设备)后,应用程序通常会在后台状态继续工作。 System or user can kill the app.系统或用户可以终止应用程序。 App can wake up after some user interaction (via geolocation services, geofencing) at wake up moment, app is restarting, and send query to sever, with checking user (authorization, gets new token etc).应用程序可以在一些用户交互(通过地理定位服务、地理围栏)后唤醒,应用程序正在重新启动,并向服务器发送查询,并检查用户(授权、获取新令牌等)。 At this moment we can see in the logs:此时我们可以在日志中看到:

> Request method: POST url: https://******/v1/account/token headers: {
    UserName = "***@***.com";
    "grant_type" = password;
    password = ****
} at time: 2016-08-15 15:30:45 +0000

code:代码:

    if ([request.method isEqualToString:@"GET"]) {
            return [manager GET:request.url parameters:request.params progress:nil success:^(NSURLSessionTask *operation, id responseObject) {

            } failure:^(NSURLSessionTask *operation, NSError *error) {

                    processError(operation, error);
                }
            }];

after some period of time user launch app, and this code trying to continue executing, and we have error with timeout.一段时间后,用户启动应用程序,并且此代码尝试继续执行,并且出现超时错误。

    error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x156d8ce10
{Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" 
UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://******/v1/account/token, NSErrorFailingURLKey=https://******/v1/account/token, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.} 
at time: 2016-08-15 15:46:28 +0000

The difference in time about 16 minutes, when user launch app again.当用户再次启动应用程序时,时间差约为 16 分钟。 And this can be in another part of http queries这可以在 http 查询的另一部分

The question is, why iOS freeze queries in background, and how to fix this.问题是,为什么 iOS 在后台冻结查询,以及如何解决这个问题。

After some time with different experiments with network, finally I found issue.经过一段时间的网络不同实验,我终于发现了问题。 After enabled CFNetwork diagnostic, we can get more info about all requests in app.启用 CFNetwork 诊断后,我们可以获得有关应用程序中所有请求的更多信息。 And CFNetwork get not -1001 error, instead all errors mark as Code=-1005 “null”, but -1005 error is "The network connection was lost". CFNetwork 得到的不是 -1001 错误,而是所有错误都标记为 Code=-1005 “null”,而 -1005 错误是“网络连接丢失”。 I don't know why AFNetworking got it this error, but this make sense.我不知道为什么 AFNetworking 会收到这个错误,但这是有道理的。 Issue begins when we connected to Wi-Fi or 3G/4G and don't have internet connection (only local), and at this moment app trying to do requests, CFNetwork alert -1005 error at this moment, but AFNetworking return error with -1001 code.当我们连接到 Wi-Fi 或 3G/4G 并且没有互联网连接(仅限本地)时,问题就开始了,此时应用程序尝试执行请求,此时 CFNetwork 警报 -1005 错误,但 AFNetworking 返回错误 - 1001 代码。 That is issue of my topic question.那是我的主题问题。

At this moment, I have not elegant solution for this, before every request trying to ping host, and if we have answer do it request.目前,我还没有优雅的解决方案,在每个请求尝试 ping 主机之前,如果我们有答案,请执行请求。 Maybe u know better way?也许你知道更好的方法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM