Do CloudKit record process loop on a high priority background thread so that it doesn't block the main thread

This commit is contained in:
Maurice Parker 2020-07-27 16:00:01 -05:00
parent b77e2164cd
commit ac9ba39ea2
1 changed files with 25 additions and 17 deletions

View File

@ -132,26 +132,34 @@ private extension CloudKitArticlesZoneDelegate {
group.leave() group.leave()
} }
let parsedItems = records.compactMap { makeParsedItem($0) } group.enter()
let webFeedIDsAndItems = Dictionary(grouping: parsedItems, by: { item in item.feedURL } ).mapValues { Set($0) } DispatchQueue.global(qos: .userInitiated).async {
for (webFeedID, parsedItems) in webFeedIDsAndItems { let parsedItems = records.compactMap { self.makeParsedItem($0) }
group.enter() let webFeedIDsAndItems = Dictionary(grouping: parsedItems, by: { item in item.feedURL } ).mapValues { Set($0) }
self.account?.update(webFeedID, with: parsedItems, deleteOlder: false) { result in
switch result { DispatchQueue.main.async {
case .success(let articleChanges): for (webFeedID, parsedItems) in webFeedIDsAndItems {
guard let deletes = articleChanges.deletedArticles, !deletes.isEmpty else { group.enter()
group.leave() self.account?.update(webFeedID, with: parsedItems, deleteOlder: false) { result in
return switch result {
case .success(let articleChanges):
guard let deletes = articleChanges.deletedArticles, !deletes.isEmpty else {
group.leave()
return
}
let syncStatuses = deletes.map { SyncStatus(articleID: $0.articleID, key: .deleted, flag: true) }
try? self.database.insertStatuses(syncStatuses)
group.leave()
case .failure(let databaseError):
errorOccurred = true
os_log(.error, log: self.log, "Error occurred while storing articles: %@", databaseError.localizedDescription)
group.leave()
}
} }
let syncStatuses = deletes.map { SyncStatus(articleID: $0.articleID, key: .deleted, flag: true) }
try? self.database.insertStatuses(syncStatuses)
group.leave()
case .failure(let databaseError):
errorOccurred = true
os_log(.error, log: self.log, "Error occurred while storing articles: %@", databaseError.localizedDescription)
group.leave()
} }
group.leave()
} }
} }
group.notify(queue: DispatchQueue.main) { group.notify(queue: DispatchQueue.main) {