NewsBlur/clients/ios/Widget Extension/WidgetLoader.swift
David Sinclair 64d1e83af7 #1607 (widget is slow to reload)
- Added logging to help check performance (look in Console, filter on "🚧")
2022-02-01 17:05:03 -08:00

67 lines
2.1 KiB
Swift

//
// WidgetLoader.swift
// Widget Extension
//
// Created by David Sinclair on 2019-11-29.
// Copyright © 2019 NewsBlur. All rights reserved.
//
import UIKit
/// Network loader for the widget.
class Loader: NSObject, URLSessionDataDelegate {
typealias Completion = (Result<Data, Error>) -> Void
private var completion: Completion
private var receivedData = Data()
init(url: URL, completion: @escaping Completion) {
self.completion = completion
super.init()
var request = URLRequest(url: url)
request.httpMethod = "GET"
// request.addValue(accept, forHTTPHeaderField: "Accept")
let config = URLSessionConfiguration.background(withIdentifier: UUID().uuidString)
config.sharedContainerIdentifier = "group.com.newsblur.NewsBlur-Group"
let session = URLSession(configuration: config, delegate: self, delegateQueue: nil)
let task = session.dataTask(with: request)
task.resume()
}
// MARK: - URL session delegate
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
guard let response = response as? HTTPURLResponse,
(200...299).contains(response.statusCode) else {
completionHandler(.cancel)
return
}
completionHandler(.allow)
}
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
NSLog("error: \(error.debugDescription)")
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
NSLog("🚧 \(dataTask.currentRequest?.url?.path ?? "?") data: \(data)")
receivedData.append(data)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let error = error {
completion(.failure(error))
} else {
completion(.success(receivedData))
}
}
}