- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
试图理解为什么在 WatchKit 3.0 中安排后台 URLSessionDownloadTask 时。函数 handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>)
从不触发 WKURLSessionRefreshBackgroundTask
, 但其他任务像 WKApplicationRefreshBackgroundTask
一样完成和 WKSnapshotRefreshBackgroundTask
.
我在 watch 中的代码 ExtensionDelegate
...
func applicationDidBecomeActive() {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
scheduleBackgroundRefresh(in: 10)
}
func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
// Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.
for task in backgroundTasks {
// Use a switch statement to check the task type
switch task {
case let backgroundTask as WKApplicationRefreshBackgroundTask:
// Be sure to complete the background task once you’re done.
scheduleURLSession()
backgroundTask.setTaskCompleted()
case let snapshotTask as WKSnapshotRefreshBackgroundTask:
// Snapshot tasks have a unique completion call, make sure to set your expiration date
snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil)
case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask:
// Be sure to complete the connectivity task once you’re done.
connectivityTask.setTaskCompleted()
case let urlSessionTask as WKURLSessionRefreshBackgroundTask: // This is never fired, Don't know why, arrrgggggg
// Be sure to complete the URL session task once you’re done.
let backgroundConfigObject = URLSessionConfiguration.background(withIdentifier: urlSessionTask.sessionIdentifier)
let backgroundSession = URLSession(configuration: backgroundConfigObject, delegate: self, delegateQueue: nil) // delegate set to self here after rejoining the session
print("Rejoining session ", backgroundSession)
urlSessionTask.setTaskCompleted() // probably need to postpone this until the url `urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)` has completed, but either way, this switch case is never met
default:
// make sure to complete unhandled task types
task.setTaskCompleted()
}
}
}
func scheduleURLSession() {
if let url = URL(string: "https://some.path.com") {
let backgroundConfigObject = URLSessionConfiguration.background(withIdentifier: NSUUID().uuidString)
let backgroundSession = URLSession(configuration: backgroundConfigObject, delegate: nil, delegateQueue: nil) // nil delegate here because delegate is "supposed" to be set in `handle(_ backgroundTasks:)` delegate method ("Rejoining session").
let task = backgroundSession.downloadTask(with: url)
task.resume()
} else {
print("Url error")
}
}
func scheduleBackgroundRefresh(in seconds: TimeInterval) {
let fireDate = Date(timeIntervalSinceNow: seconds)
// optional, any SecureCoding compliant data can be passed here
let userInfo = ["reason" : "background update"] as NSDictionary
WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: fireDate, userInfo: userInfo) { (error) in
if (error == nil) {
print("successfully scheduled background task, use the crown to send the app to the background and wait for handle:BackgroundTasks to fire.")
}
}
}
Here's Apple's sample code for reference
知道为什么 URLSessionDownloadTask
在后台永远不会触发 handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>)
开火?
最佳答案
您已将 preferredFireDate 设置为 10 秒。正如苹果所说,不能保证这么快就触发处理方法。
preferredFireDate
The time of the next background snapshot refresh task. The system makes every effort to wake your app in the background at some point after the scheduled time, but the precise time is not guaranteed.
当我将此参数设置为 30 秒时,我不得不等待大约 10 分钟,直到系统调用 handle 方法。
您还必须为 WKExtension 设置委托(delegate)。
WKExtension.shared().delegate = self//self是一个实现handle方法的对象
编辑
Background app refresh tasks are budgeted. In general, the system performs approximately one task per hour for each app in the dock (including the most recently used app). This budget is shared among all apps on the dock. The system performs multiple tasks an hour for each app with a complication on the active watch face. This budget is shared among all complications on the watch face. After you exhaust the budget, the system delays your requests until more time becomes available.
关于ios - 后台的 URLSessionDownloadTask 从不触发 handle(_ backgroundTasks : Set<WKRefreshBackgroundTask>) for WKURLSessionRefreshBackgroundTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43035869/
我创建了一个类来使用 URLSession 下载文件。 我注意到一个奇怪的行为,当设备处于 Debug模式时,它正在下载文件。但是,如果我删除设备并手动运行应用程序,则下载根本无法进行。有时重新连接设
目前我正在努力从服务器下载一个文件,而且效果很好。 我的问题是在实际下载开始之前我怎么知道 url 是否具有恢复能力? 下面是一些代码片段, class Downloader:NSObject,URL
NSURLSessionDownloadDelegate协议(protocol)中有3个方法。 URLSession:downloadTask:didFinishDownloadingToURL:,
当我尝试从 url 下载文件时,文件会被下载并存储在文件夹中。但是当我尝试打开该文件时。该文件似乎已损坏,我无法打开该 pdf 文件。 我已经使用下面提到的代码尝试了所有可能性。但它们都不起作用。任何
当应用程序处于关闭状态或我的下载状态发生变化时,例如从等待状态到下载状态或完成状态,我想在核心数据中保存 URLSessionDownloadTask。 我的自定义类的所有其他属性都完美存储,但应用程
试图理解为什么在 WatchKit 3.0 中安排后台 URLSessionDownloadTask 时。函数 handle(_ backgroundTasks: Set)从不触发 WKURLSess
我是一名优秀的程序员,十分优秀!