2019-07-06 05:06:31 +02:00
|
|
|
//
|
|
|
|
// FetchRequestOperation.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 6/20/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSCore
|
|
|
|
import Account
|
|
|
|
import Articles
|
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
// Runs an asynchronous fetch.
|
|
|
|
|
|
|
|
typealias FetchRequestOperationResultBlock = (Set<Article>, FetchRequestOperation) -> Void
|
|
|
|
|
|
|
|
class FetchRequestOperation {
|
|
|
|
|
|
|
|
let id: Int
|
|
|
|
let resultBlock: FetchRequestOperationResultBlock
|
|
|
|
var isCanceled = false
|
|
|
|
var isFinished = false
|
|
|
|
private let representedObjects: [Any]
|
|
|
|
|
|
|
|
init(id: Int, representedObjects: [Any], resultBlock: @escaping FetchRequestOperationResultBlock) {
|
|
|
|
precondition(Thread.isMainThread)
|
|
|
|
self.id = id
|
|
|
|
self.representedObjects = representedObjects
|
|
|
|
self.resultBlock = resultBlock
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(_ completion: @escaping (FetchRequestOperation) -> Void) {
|
|
|
|
precondition(Thread.isMainThread)
|
|
|
|
precondition(!isFinished)
|
|
|
|
|
|
|
|
if isCanceled {
|
|
|
|
completion(self)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let articleFetchers = representedObjects.compactMap{ $0 as? ArticleFetcher }
|
|
|
|
if articleFetchers.isEmpty {
|
|
|
|
isFinished = true
|
|
|
|
resultBlock(Set<Article>(), self)
|
|
|
|
completion(self)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let numberOfFetchers = articleFetchers.count
|
|
|
|
var fetchersReturned = 0
|
|
|
|
var fetchedArticles = Set<Article>()
|
|
|
|
for articleFetcher in articleFetchers {
|
2019-09-04 07:58:17 +02:00
|
|
|
var didCallCompletion = false
|
2019-07-06 05:06:31 +02:00
|
|
|
articleFetcher.fetchArticlesAsync { (articles) in
|
|
|
|
precondition(Thread.isMainThread)
|
|
|
|
if self.isCanceled {
|
2019-09-04 07:58:17 +02:00
|
|
|
if !didCallCompletion {
|
|
|
|
didCallCompletion = true
|
|
|
|
completion(self)
|
|
|
|
}
|
2019-07-06 05:06:31 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fetchedArticles.formUnion(articles)
|
|
|
|
fetchersReturned += 1
|
|
|
|
if fetchersReturned == numberOfFetchers {
|
|
|
|
self.isFinished = true
|
|
|
|
self.resultBlock(fetchedArticles, self)
|
|
|
|
completion(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|