简体   繁体   English

iOS 7后台传输服务在3分钟后停止

[英]iOS 7 Background transfer service stops after 3 minutes

I have created a sample code to download a file from network repeatedly(every 30 secs or so). 我已经创建了一个示例代码,用于重复从网络下载文件(每30秒左右)。 In iOS 7 using Background transfer services with NSURLSession 在iOS 7中使用NSURLSession的后台传输服务
I followed this tutorial http://mobile.tutsplus.com/tutorials/iphone/ios-7-sdk-background-transfer-service/ and added a timer like this to repeat it. 我按照本教程http://mobile.tutsplus.com/tutorials/iphone/ios-7-sdk-background-transfer-service/添加了一个这样的计时器来重复它。

    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
mute = [NSTimer scheduledTimerWithTimeInterval:30.0f
                                        target:self
                                      selector:@selector(startDownload)
                                      userInfo:nil
                                       repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:mute forMode:NSRunLoopCommonModes];

When I run it(in background by clicking home button) in simulator and on iPad connected to the Xcode(where I can see the logs) everything works fine and it keeps downloading continuously. 当我在模拟器中运行它(在后台通过单击主页按钮)和连接到Xcode的iPad(我可以看到日志)时,一切正常并且它会持续下载。 But when I disconnect the iPad from the Mac and run it on the iPad in background after around 3 seconds it stops running (handleEventsForBackgroundURLSession in AppDelegate get called). 但是当我将iPad与Mac断开连接并在大约3秒后在后台运行iPad时它会停止运行(调用AppDelegate中的handleEventsForBackgroundURLSession)。

In Xcode project capabilities I have selected Background fetch as Background Modes. 在Xcode项目功能中,我选择了背景提取作为背景模式。

What am I missing here or what have I done wrong so that it gets stop after around 3 minutes? 我在这里错过了什么或者我做错了什么,以便在大约3分钟后停止? (According to the documentations with iOS 7 Background transfer services it should run continuously as there's no time limit in background for this.) (根据iOS 7后台传输服务的文档,它应该连续运行,因为后台没有时间限制。)

Thanks 谢谢

Background tasks in iOS7 will only give you 30 seconds at most (dramatically down from 10 minutes). iOS7中的后台任务最多只能给你30秒(从10分钟开始大幅下降)。 You should use the new fetch background mode instead. 您应该使用新的获取后台模式。 You should not be using a timer, but use the newly provided API to ask the OS to be woken up in regular intervals and set up your download using NSURLSession . 您不应该使用计时器,而是使用新提供的API要求定期唤醒操作系统并使用NSURLSession设置下载。

Background Fetch is something different. 背景提取是不同的东西。 Background Fetch will wake up your app for periodic fetches of new data (typically, a < 30s network request looking for updates). 后台Fetch将唤醒您的应用程序以定期提取新数据(通常,<30s网络请求正在查找更新)。 This is not related to background NSURLSessions and should probably be turned off if you aren't actually using it for queuing NSURLSessionDownloadTasks or other update purposes. 这与后台NSURLSession无关,如果您实际上没有使用它来排队NSURLSessionDownloadTasks或其他更新目的,则应该关闭它。

From what I understand, it's possible that the behavior you are seeing is actually normal. 根据我的理解,您所看到的行为可能实际上是正常的。 I do not believe that background NSURLSessionDownloadTasks are guaranteed to run continuously or on any device configuration (AC vs battery, WiFi vs Cellular, etc.) You said that you disconnected from your Mac which would switch device state from charging to battery. 我不相信背景NSURLSessionDownloadTasks可以保证连续运行或在任何设备配置上运行(AC与电池,WiFi与蜂窝电话等)你说你断开了Mac,这会将设备状态从充电切换到电池。 Among other factors, that could be enough to pause transfers or decrease download limits. 除其他因素外,这可能足以暂停传输或降低下载限制。 Unfortunately, this system appears to be very opaque to developers. 不幸的是,这个系统似乎对开发人员来说非常不透明。

To be sure you are getting the highest priority available, make sure your discretionary property on your NSURLSessionConfiguration is set to false . 为确保获得最高优先级,请确保NSURLSessionConfiguration上的discretionary属性设置为false Download tasks created while the app is in the background will always have this set to true , so just an FYI there. 在应用程序处于后台时创建的下载任务将始终设置为true ,因此只有一个FYI。

Apple's sample code will put you on the right track: https://developer.apple.com/library/iOS/samplecode/SimpleBackgroundTransfer/Introduction/Intro.html Apple的示例代码将使您走上正轨: https//developer.apple.com/library/iOS/samplecode/SimpleBackgroundTransfer/Introduction/Intro.html

Try the below steps. 请尝试以下步骤。 This worked fine for me. 这对我来说很好。

In your .h 在你的.h

UIBackgroundTaskIdentifier bgTask;

In .m 在.m

//background task code
UIApplication *app = [UIApplication sharedApplication];

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

[NSTimer scheduledTimerWithTimeInterval:30.0f target:self selector:@selector(startDownload) userInfo:nil repeats:YES];

And yor method for downloading is 你下载的方法是

-(void)startDownload{

    NSLog(@"will log even if in background or foreground");
}

Practically using [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler: ^{}]; 实际使用[[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler: ^{}]; will give you only 180 sec.Xcode provides the facility of background active app while debugging only just to ease developers. 将只给你180秒.Xcode提供后台活动应用程序的功能,而调试只是为了方便开发人员。

NSURLSession download tasks are indeed not bound to time restrictions. NSURLSession下载任务确实不受时间限制。 But they are completely managed by the system. 但它们完全由系统管理。 They may get postponed if the system resources are low. 如果系统资源不足,它们可能会被推迟。 They are normally dialed down when there is no wifi or when the device is not plugged. 通常在没有wifi或没有插入设备时拨打它们。 The system also observes your app's use of the background transfer services and may treat it with lower priority if it detects that it abuses the feature. 系统还会观察您的应用程序对后台传输服务的使用,如果它检测到它滥用该功能,则可能会以较低的优先级对其进行处理。 A background transfer every 30 seconds, is certainly considered an abuse. 每30秒进行一次背景传输,肯定被认为是滥用。

I suggest dropping the use of Background Transfer Services and set up the entire thing using background fetch (which is a completely different thing btw). 我建议放弃使用后台传输服务并使用后台获取来设置整个事物(这是一个完全不同的事情)。 Just be careful if you want to reach the store, you must fall in one of the accepted uses cases for the feature to be approved for your app. 如果您想要到达商店,请务必小心,您必须属于一个已接受的用例,才能为您的应用批准该功能。 If not, then there is little hope. 如果没有,那么就没有希望了。 Not sure what you are trying to do. 不确定你要做什么。 Maybe you don't really need to have so much background activity. 也许你真的不需要那么多背景活动。

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

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