2019-06-17 14:49:23 -07:00
|
|
|
//
|
2019-07-30 17:00:04 -07:00
|
|
|
// FetchSourceOperation.swift
|
2019-06-17 14:49:23 -07:00
|
|
|
// AltStore
|
|
|
|
|
//
|
2019-07-30 17:00:04 -07:00
|
|
|
// Created by Riley Testut on 7/30/19.
|
2019-06-17 14:49:23 -07:00
|
|
|
// Copyright © 2019 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import Roxas
|
|
|
|
|
|
2019-07-30 17:00:04 -07:00
|
|
|
@objc(FetchSourceOperation)
|
|
|
|
|
class FetchSourceOperation: ResultOperation<Source>
|
2019-06-17 14:49:23 -07:00
|
|
|
{
|
2019-07-30 17:00:04 -07:00
|
|
|
let sourceURL: URL
|
|
|
|
|
|
2019-06-17 14:49:23 -07:00
|
|
|
private let session = URLSession(configuration: .default)
|
|
|
|
|
|
|
|
|
|
private lazy var dateFormatter: DateFormatter = {
|
|
|
|
|
let dateFormatter = DateFormatter()
|
|
|
|
|
dateFormatter.dateFormat = "yyyy-MM-dd"
|
|
|
|
|
return dateFormatter
|
|
|
|
|
}()
|
|
|
|
|
|
2019-07-30 17:00:04 -07:00
|
|
|
init(sourceURL: URL)
|
|
|
|
|
{
|
|
|
|
|
self.sourceURL = sourceURL
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 14:49:23 -07:00
|
|
|
override func main()
|
|
|
|
|
{
|
|
|
|
|
super.main()
|
|
|
|
|
|
2019-07-30 17:00:04 -07:00
|
|
|
let dataTask = self.session.dataTask(with: self.sourceURL) { (data, response, error) in
|
2019-06-17 14:49:23 -07:00
|
|
|
DatabaseManager.shared.persistentContainer.performBackgroundTask { (context) in
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
let (data, _) = try Result((data, response), error).get()
|
|
|
|
|
|
|
|
|
|
let decoder = JSONDecoder()
|
|
|
|
|
decoder.dateDecodingStrategy = .formatted(self.dateFormatter)
|
|
|
|
|
decoder.managedObjectContext = context
|
|
|
|
|
|
2019-07-30 17:00:04 -07:00
|
|
|
let source = try decoder.decode(Source.self, from: data)
|
|
|
|
|
self.finish(.success(source))
|
2019-06-17 14:49:23 -07:00
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
self.finish(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.progress.addChild(dataTask.progress, withPendingUnitCount: 1)
|
|
|
|
|
|
|
|
|
|
dataTask.resume()
|
|
|
|
|
}
|
|
|
|
|
}
|