简体   繁体   English

应用程序在后台时下载网络数据

[英]Downloading network data while app is in background

I am using coreLocation with an app that is registered for location updates in the background.我正在使用 coreLocation 和一个在后台注册位置更新的应用程序。 Can I make network calls to a web service to download and store data while the app is in the background?当应用程序在后台时,我可以对 Web 服务进行网络调用以下载和存储数据吗? I am having problems determining how much I can do in the background.我在确定我可以在后台执行多少操作时遇到问题。

As an example, when the user makes a significant change to his/her location, could I make a call to a weather service requesting and logging the weather for that location (That's not exactly what I'll be doing but as an example of a web service)?例如,当用户对他/她的位置进行重大更改时,我是否可以调用天气服务来请求并记录该位置的天气(这不是我将要做的事情,而是作为一个例子)网络服务)? Resulting in a list of places he/she has been and what the weather was at the time.结果是他/她去过的地方的列表以及当时的天气。

Can I make Network connections and requests while in the background to download data?我可以在后台进行网络连接和请求以下载数据吗? Or am I limited somehow??还是我有什么限制?? Or am I just limited by the amount of time my process takes.或者我只是受到我的过程所花费的时间的限制。

I do understand the pitfalls of battery and network charges if an app is continually doing this.如果应用程序不断这样做,我确实了解电池和网络费用的陷阱。 That for now is beside the point.这暂时不是重点。 Just looking for specifics on downloading data while the app is in the background.只是在应用程序处于后台时寻找有关下载数据的细节。

Thanks in advance -提前致谢 -

Your app is not necessarily running in the background just because you registered for the location service in the background.您的应用程序不一定会在后台运行,因为您在后台注册了定位服务。 It is essentially woken up to process your delegate calls when your location manager sends a location event.当您的位置管理器发送位置事件时,它基本上被唤醒以处理您的委托调用。 If your location doesn't change for some time, the app will be suspended and may even be terminated.如果您的位置在一段时间内没有改变,该应用程序将被暂停,甚至可能会被终止。 The location manager, will wake it up and you will have a limited time to do work when a new location event is processed.位置管理器将唤醒它,当处理新的位置事件时,您将有有限的时间进行工作。

If you initiate a call for refresh data, wrapped in beginBackgroundTaskIdentifier and endBackgroundTaskIdentifier calls, you should get a decent chance to process the network data method.如果您发起刷新数据调用,包含在 beginBackgroundTaskIdentifier 和 endBackgroundTaskIdentifier 调用中,您应该有机会处理网络数据方法。 I don't think that you can start that process or count on it being processed at any other time while in the background and possibly suspended.我不认为您可以启动该进程或指望它在后台的任何其他时间被处理并可能暂停。

The link that psoft references will give you a better idea on how to manage the process while in the background. psoft 引用的链接将使您更好地了解如何在后台管理进程。

The system can and will tear down network connections when it suspends your app.当系统挂起你的应用程序时,它可以并且将会断开网络连接。 I'm guessing that your existing connections will get some time to finish their work, either during the 'Inactive' (transition) state, or while still in the 'Background' but before the 'Suspended' state.我猜您现有的连接将有一些时间来完成他们的工作,无论是在“非活动”(转换)状态期间,还是在“后台”但在“暂停”状态之前。 Also, you can request more time in 'Background' before transitioning to 'Suspended'.此外,您可以在过渡到“暂停”之前在“背景”中请求更多时间。 Read "Background Execution and Multitasking" here . 在此处阅读“后台执行和多任务处理”。 The only type of service can maintain network connections are Newsstand downloads and VOIP apps.唯一可以保持网络连接的服务类型是 Newsstand 下载和 VOIP 应用程序。

In my app which downloads large PDFs, I simply use this code to continue downloading the PDFs for up to 10 minutes when the user hits their home button and puts the app into a background state:在我下载大型 PDF 的应用程序中,当用户点击主页按钮并将应用程序置于后台状态时,我只需使用此代码继续下载 PDF 长达 10 分钟:

- (void)applicationDidEnterBackground:(UIApplication *)application 
{
    UIApplication  *app = [UIApplication sharedApplication];
    UIBackgroundTaskIdentifier bgTask;

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
    [app endBackgroundTask:bgTask]; 
    }];
}

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

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