Move CloudKit record creation to a high priority background queue so that it isn't blocking main

This commit is contained in:
Maurice Parker 2020-07-27 16:08:09 -05:00
parent ac9ba39ea2
commit 8e890ec177
1 changed files with 31 additions and 28 deletions

View File

@ -114,35 +114,38 @@ final class CloudKitArticlesZone: CloudKitZone {
var newRecords = [CKRecord]()
var deleteRecordIDs = [CKRecord.ID]()
for statusUpdate in statusUpdates {
switch statusUpdate.record {
case .all:
modifyRecords.append(makeStatusRecord(statusUpdate))
modifyRecords.append(makeArticleRecord(statusUpdate.article!))
case .new:
newRecords.append(makeStatusRecord(statusUpdate))
newRecords.append(makeArticleRecord(statusUpdate.article!))
case .delete:
deleteRecordIDs.append(CKRecord.ID(recordName: statusID(statusUpdate.articleID), zoneID: Self.zoneID))
case .statusOnly:
modifyRecords.append(makeStatusRecord(statusUpdate))
deleteRecordIDs.append(CKRecord.ID(recordName: articleID(statusUpdate.articleID), zoneID: Self.zoneID))
}
}
modify(recordsToSave: modifyRecords, recordIDsToDelete: deleteRecordIDs) { result in
switch result {
case .success:
self.saveIfNew(newRecords) { result in
switch result {
case .success:
completion(.success(()))
case .failure(let error):
completion(.failure(error))
}
DispatchQueue.global(qos: .userInitiated).async {
for statusUpdate in statusUpdates {
switch statusUpdate.record {
case .all:
modifyRecords.append(self.makeStatusRecord(statusUpdate))
modifyRecords.append(self.makeArticleRecord(statusUpdate.article!))
case .new:
newRecords.append(self.makeStatusRecord(statusUpdate))
newRecords.append(self.makeArticleRecord(statusUpdate.article!))
case .delete:
deleteRecordIDs.append(CKRecord.ID(recordName: self.statusID(statusUpdate.articleID), zoneID: Self.zoneID))
case .statusOnly:
modifyRecords.append(self.makeStatusRecord(statusUpdate))
deleteRecordIDs.append(CKRecord.ID(recordName: self.articleID(statusUpdate.articleID), zoneID: Self.zoneID))
}
}
self.modify(recordsToSave: modifyRecords, recordIDsToDelete: deleteRecordIDs) { result in
switch result {
case .success:
self.saveIfNew(newRecords) { result in
switch result {
case .success:
completion(.success(()))
case .failure(let error):
completion(.failure(error))
}
}
case .failure(let error):
self.handleModifyArticlesError(error, statusUpdates: statusUpdates, completion: completion)
}
case .failure(let error):
self.handleModifyArticlesError(error, statusUpdates: statusUpdates, completion: completion)
}
}
}