我有一个BackgroundSession类的文件
class BackgroundSession: NSObject {
static let shared = BackgroundSession()
static let identifier = "com.***.bg"
private var session: URLSession!
var savedCompletionHandler: (() -> Void)?
private override init() {
super.init()
let configuration = URLSessionConfiguration.background(withIdentifier: BackgroundSession.identifier)
session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
}
func start(_ request: URLRequest) {
session.downloadTask(with: request).resume()
}
}
extension BackgroundSession: URLSessionDelegate {
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
DispatchQueue.main.async {
self.savedCompletionHandler?()
self.savedCompletionHandler = nil
}
}
}
extension BackgroundSession: URLSessionTaskDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let error = error {
// handle failure here
print("\(error.localizedDescription)")
}
}
}
extension BackgroundSession: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
do {
let data = try Data(contentsOf: location)
let json = try JSONSerialization.jsonObject(with: data)
print("\(json)")
// do something with json
} catch {
print("\(error.localizedDescription)")
}
}
}
我正在监听后台位置更新,稍后再发布
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("didUpdateLocations")
if locations.first != nil {
let lastLocation = locations.last
self.lastLocation = lastLocation
print("doing background work")
self.getUserData()
if PubnubController.pubnubChannel != nil {
PubnubController.sharedClient.publish(["action": "newCoordinates", "data": ["coordinates": ["latitude": lastLocation?.coordinate.latitude, "longitude": lastLocation?.coordinate.longitude]]], toChannel: PubnubController.pubnubChannel!, compressed: false)
}
}
}
self.getUserData()
看起来像这样
func getUserData() {
print("getUserData")
if (self.userId != -1 && self.userAuthToken != nil) {
let httpUrl: String = "https://api.***.com/dev/users/\(self.userId)"
guard let url = URL(string: httpUrl) else {
return
}
var request = URLRequest(url: url)
request.setValue(self.userAuthToken, forHTTPHeaderField: "Authorization")
let session = BackgroundSession.shared
session.start(request)
}
}
在我的ExtensionDelegate.swift
中,我有典型的func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>)
具有for
循环,并且switch
设置了WKURLSessionRefreshBackgroundTask
的大小写,
case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
print("WKURLSessionRefreshBackgroundTask")
// Be sure to complete the URL session task once you’re done.
urlSessionTask.setTaskCompletedWithSnapshot(false)
在我的控制器中,我还粘贴了该类应该调用的函数
func application(_ application: WKExtension, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
print("handleEventsForBackgroundURLSession")
BackgroundSession.shared.savedCompletionHandler = parseUserData
}
似乎我的数据没有同时调用委托函数和此粘贴函数。我很难理解背景URLSession流程
请注意BackgroundSession
类来自这个Stackoverflow问题
URLSession.datatask with request block not called in background
答案 0 :(得分:0)
此handleEventsForBackgroundURLSession
是一种iOS模式。这是UIApplicationDelegate
协议的方法。您不能只是将其添加到某个随机控制器中。它仅适用于您的iOS应用的UIApplicationDelegate
。
对于watchOS,我怀疑这个想法是相同的,除了调用iOS {{1}的BackgroundSession
的{{1}}而不是调用iOS提供的完成处理程序之外, }}:
setTaskCompletedWithSnapshot
但是,实际上,想法是相同的。我们将WKURLSessionRefreshBackgroundTask
推迟到调用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 urlSessionTask as WKURLSessionRefreshBackgroundTask:
// Be sure to complete the URL session task once you’re done.
BackgroundSession.shared.savedCompletionHandler = {
urlSessionTask.setTaskCompletedWithSnapshot(false)
}
...
}
}
}
为止。
如果您希望setTaskCompletedWithSnapshot
调用控制器的解析器,则可以为该接口指定协议:
urlSessionDidFinishEvents(forBackgroundURLSession:)
然后您可以给BackgroundSession
一个属性来跟踪解析器:
protocol Parser: class {
func parse(_ data: Data)
}
您可以让BackgroundSession
调用解析器:
weak var parser: Parser?
然后,您可以让您的控制器(或其他任何控制器)(a)符合此协议; (b)实施该协议的didFinishDownloadingTo
方法;和(c)将自身指定为解析器:
extension BackgroundSession: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
do {
let data = try Data(contentsOf: location)
parser?.parse(data)
} catch {
os_log(.error, log: log, "Error retrieving data for %{public}@: %{public}@", downloadTask.originalRequest?.url?.absoluteString ?? "Unknown request", error.localizedDescription)
}
}
}