Add missing story fetching

This commit is contained in:
Anh Do 2020-03-14 17:51:21 -04:00
parent bde6a607ba
commit 1d3073c593
No known key found for this signature in database
GPG Key ID: 451E3092F917B62D
1 changed files with 52 additions and 2 deletions

View File

@ -239,8 +239,58 @@ final class NewsBlurAccountDelegate: AccountDelegate {
}
func refreshMissingStories(for account: Account, completion: @escaping (Result<Void, Error>)-> Void) {
os_log(.debug, log: log, "Refreshing missing stories...")
account.fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDate { result in
func process(_ fetchedHashes: Set<String>) {
let group = DispatchGroup()
var errorOccurred = false
let storyHashes = Array(fetchedHashes).map { NewsBlurStoryHash(hash: $0, timestamp: Date()) }
let chunkedStoryHashes = storyHashes.chunked(into: 100)
for chunk in chunkedStoryHashes {
group.enter()
self.caller.retrieveStories(hashes: chunk) { result in
switch result {
case .success(let stories):
self.processStories(account: account, stories: stories) { error in
group.leave()
if error != nil {
errorOccurred = true
}
}
case .failure(let error):
errorOccurred = true
os_log(.error, log: self.log, "Refresh missing stories failed: %@.", error.localizedDescription)
group.leave()
}
}
}
group.notify(queue: DispatchQueue.main) {
self.refreshProgress.completeTask()
os_log(.debug, log: self.log, "Done refreshing missing stories.")
if errorOccurred {
completion(.failure(NewsBlurError.unknown))
} else {
completion(.success(()))
}
}
}
switch result {
case .success(let fetchedArticleIDs):
process(fetchedArticleIDs)
case .failure(let error):
self.refreshProgress.completeTask()
completion(.failure(error))
}
}
}
func processStories(account: Account, stories: [NewsBlurStory]?, completion: @escaping DatabaseCompletionBlock) {
let parsedItems = mapStoriesToParsedItems(stories: stories)