Correct number of download items to tick

This commit is contained in:
Maurice Parker 2020-04-26 10:05:26 -05:00
parent e68383da09
commit 09addec369
2 changed files with 54 additions and 17 deletions

View File

@ -197,7 +197,7 @@ private extension CloudKitAcountZoneDelegate {
if let feedProvider = FeedProviderManager.shared.best(for: urlComponents) { if let feedProvider = FeedProviderManager.shared.best(for: urlComponents) {
refreshProgress?.addToNumberOfTasksAndRemaining(4) refreshProgress?.addToNumberOfTasksAndRemaining(5)
feedProvider.assignName(urlComponents) { result in feedProvider.assignName(urlComponents) { result in
self.refreshProgress?.completeTask() self.refreshProgress?.completeTask()
switch result { switch result {
@ -222,6 +222,7 @@ private extension CloudKitAcountZoneDelegate {
self.articlesZone?.deleteArticles(deletedArticles) { _ in self.articlesZone?.deleteArticles(deletedArticles) { _ in
self.refreshProgress?.completeTask() self.refreshProgress?.completeTask()
self.articlesZone?.saveNewArticles(newAndUpdatedArticles) { _ in self.articlesZone?.saveNewArticles(newAndUpdatedArticles) { _ in
self.refreshProgress?.completeTask()
self.articlesZone?.fetchChangesInZone() { _ in self.articlesZone?.fetchChangesInZone() { _ in
self.refreshProgress?.completeTask() self.refreshProgress?.completeTask()
completion(webFeed) completion(webFeed)
@ -245,7 +246,7 @@ private extension CloudKitAcountZoneDelegate {
} else { } else {
refreshProgress?.addToNumberOfTasksAndRemaining(3) refreshProgress?.addToNumberOfTasksAndRemaining(4)
BatchUpdate.shared.start() BatchUpdate.shared.start()
InitialFeedDownloader.download(url) { parsedFeed in InitialFeedDownloader.download(url) { parsedFeed in
@ -267,6 +268,7 @@ private extension CloudKitAcountZoneDelegate {
self.articlesZone?.deleteArticles(deletedArticles) { _ in self.articlesZone?.deleteArticles(deletedArticles) { _ in
self.refreshProgress?.completeTask() self.refreshProgress?.completeTask()
self.articlesZone?.saveNewArticles(newAndUpdatedArticles) { _ in self.articlesZone?.saveNewArticles(newAndUpdatedArticles) { _ in
self.refreshProgress?.completeTask()
self.articlesZone?.fetchChangesInZone() { _ in self.articlesZone?.fetchChangesInZone() { _ in
self.refreshProgress?.completeTask() self.refreshProgress?.completeTask()
completion(webFeed) completion(webFeed)

View File

@ -130,6 +130,11 @@ extension CloudKitZone {
} }
database.perform(query, inZoneWith: Self.zoneID) { [weak self] records, error in database.perform(query, inZoneWith: Self.zoneID) { [weak self] records, error in
guard let self = self else {
completion(.failure(CloudKitZoneError.unknown))
return
}
switch CloudKitZoneResult.resolve(error) { switch CloudKitZoneResult.resolve(error) {
case .success: case .success:
DispatchQueue.main.async { DispatchQueue.main.async {
@ -140,10 +145,10 @@ extension CloudKitZone {
} }
} }
case .zoneNotFound: case .zoneNotFound:
self?.createZoneRecord() { result in self.createZoneRecord() { result in
switch result { switch result {
case .success: case .success:
self?.query(query, completion: completion) self.query(query, completion: completion)
case .failure(let error): case .failure(let error):
DispatchQueue.main.async { DispatchQueue.main.async {
completion(.failure(error)) completion(.failure(error))
@ -151,8 +156,9 @@ extension CloudKitZone {
} }
} }
case .retry(let timeToWait): case .retry(let timeToWait):
self?.retryIfPossible(after: timeToWait) { os_log(.error, log: self.log, "%@ zone query retry in %@ seconds.", Self.zoneID.zoneName, timeToWait)
self?.query(query, completion: completion) self.retryIfPossible(after: timeToWait) {
self.query(query, completion: completion)
} }
case .userDeletedZone: case .userDeletedZone:
DispatchQueue.main.async { DispatchQueue.main.async {
@ -176,6 +182,11 @@ extension CloudKitZone {
let recordID = CKRecord.ID(recordName: externalID, zoneID: Self.zoneID) let recordID = CKRecord.ID(recordName: externalID, zoneID: Self.zoneID)
database?.fetch(withRecordID: recordID) { [weak self] record, error in database?.fetch(withRecordID: recordID) { [weak self] record, error in
guard let self = self else {
completion(.failure(CloudKitZoneError.unknown))
return
}
switch CloudKitZoneResult.resolve(error) { switch CloudKitZoneResult.resolve(error) {
case .success: case .success:
DispatchQueue.main.async { DispatchQueue.main.async {
@ -186,10 +197,10 @@ extension CloudKitZone {
} }
} }
case .zoneNotFound: case .zoneNotFound:
self?.createZoneRecord() { result in self.createZoneRecord() { result in
switch result { switch result {
case .success: case .success:
self?.fetch(externalID: externalID, completion: completion) self.fetch(externalID: externalID, completion: completion)
case .failure(let error): case .failure(let error):
DispatchQueue.main.async { DispatchQueue.main.async {
completion(.failure(error)) completion(.failure(error))
@ -197,8 +208,9 @@ extension CloudKitZone {
} }
} }
case .retry(let timeToWait): case .retry(let timeToWait):
self?.retryIfPossible(after: timeToWait) { os_log(.error, log: self.log, "%@ zone fetch retry in %@ seconds.", Self.zoneID.zoneName, timeToWait)
self?.fetch(externalID: externalID, completion: completion) self.retryIfPossible(after: timeToWait) {
self.fetch(externalID: externalID, completion: completion)
} }
case .userDeletedZone: case .userDeletedZone:
DispatchQueue.main.async { DispatchQueue.main.async {
@ -229,8 +241,10 @@ extension CloudKitZone {
op.isAtomic = false op.isAtomic = false
op.modifyRecordsCompletionBlock = { [weak self] (_, _, error) in op.modifyRecordsCompletionBlock = { [weak self] (_, _, error) in
guard let self = self else {
guard let self = self else { return } completion(.failure(CloudKitZoneError.unknown))
return
}
switch CloudKitZoneResult.resolve(error) { switch CloudKitZoneResult.resolve(error) {
case .success: case .success:
@ -253,6 +267,7 @@ extension CloudKitZone {
completion(.failure(CloudKitZoneError.userDeletedZone)) completion(.failure(CloudKitZoneError.userDeletedZone))
} }
case .retry(let timeToWait): case .retry(let timeToWait):
os_log(.error, log: self.log, "%@ zone save if new retry in %@ seconds.", Self.zoneID.zoneName, timeToWait)
self.retryIfPossible(after: timeToWait) { self.retryIfPossible(after: timeToWait) {
self.saveIfNew(records, completion: completion) self.saveIfNew(records, completion: completion)
} }
@ -294,7 +309,12 @@ extension CloudKitZone {
/// Save the CKSubscription /// Save the CKSubscription
func save(_ subscription: CKSubscription, completion: @escaping (Result<CKSubscription, Error>) -> Void) { func save(_ subscription: CKSubscription, completion: @escaping (Result<CKSubscription, Error>) -> Void) {
database?.save(subscription) { savedSubscription, error in database?.save(subscription) { [weak self] savedSubscription, error in
guard let self = self else {
completion(.failure(CloudKitZoneError.unknown))
return
}
switch CloudKitZoneResult.resolve(error) { switch CloudKitZoneResult.resolve(error) {
case .success: case .success:
DispatchQueue.main.async { DispatchQueue.main.async {
@ -312,6 +332,7 @@ extension CloudKitZone {
} }
} }
case .retry(let timeToWait): case .retry(let timeToWait):
os_log(.error, log: self.log, "%@ zone save subscription retry in %@ seconds.", Self.zoneID.zoneName, timeToWait)
self.retryIfPossible(after: timeToWait) { self.retryIfPossible(after: timeToWait) {
self.save(subscription, completion: completion) self.save(subscription, completion: completion)
} }
@ -346,13 +367,19 @@ extension CloudKitZone {
/// Delete a CKSubscription /// Delete a CKSubscription
func delete(subscriptionID: String, completion: @escaping (Result<Void, Error>) -> Void) { func delete(subscriptionID: String, completion: @escaping (Result<Void, Error>) -> Void) {
database?.delete(withSubscriptionID: subscriptionID) { _, error in database?.delete(withSubscriptionID: subscriptionID) { [weak self] _, error in
guard let self = self else {
completion(.failure(CloudKitZoneError.unknown))
return
}
switch CloudKitZoneResult.resolve(error) { switch CloudKitZoneResult.resolve(error) {
case .success: case .success:
DispatchQueue.main.async { DispatchQueue.main.async {
completion(.success(())) completion(.success(()))
} }
case .retry(let timeToWait): case .retry(let timeToWait):
os_log(.error, log: self.log, "%@ zone delete subscription retry in %@ seconds.", Self.zoneID.zoneName, timeToWait)
self.retryIfPossible(after: timeToWait) { self.retryIfPossible(after: timeToWait) {
self.delete(subscriptionID: subscriptionID, completion: completion) self.delete(subscriptionID: subscriptionID, completion: completion)
} }
@ -372,7 +399,10 @@ extension CloudKitZone {
op.modifyRecordsCompletionBlock = { [weak self] (_, _, error) in op.modifyRecordsCompletionBlock = { [weak self] (_, _, error) in
guard let self = self else { return } guard let self = self else {
completion(.failure(CloudKitZoneError.unknown))
return
}
switch CloudKitZoneResult.resolve(error) { switch CloudKitZoneResult.resolve(error) {
case .success: case .success:
@ -395,6 +425,7 @@ extension CloudKitZone {
completion(.failure(CloudKitZoneError.userDeletedZone)) completion(.failure(CloudKitZoneError.userDeletedZone))
} }
case .retry(let timeToWait): case .retry(let timeToWait):
os_log(.error, log: self.log, "%@ zone modify retry in %@ seconds.", Self.zoneID.zoneName, timeToWait)
self.retryIfPossible(after: timeToWait) { self.retryIfPossible(after: timeToWait) {
self.modify(recordsToSave: recordsToSave, recordIDsToDelete: recordIDsToDelete, completion: completion) self.modify(recordsToSave: recordsToSave, recordIDsToDelete: recordIDsToDelete, completion: completion)
} }
@ -466,7 +497,10 @@ extension CloudKitZone {
} }
op.fetchRecordZoneChangesCompletionBlock = { [weak self] error in op.fetchRecordZoneChangesCompletionBlock = { [weak self] error in
guard let self = self else { return } guard let self = self else {
completion(.failure(CloudKitZoneError.unknown))
return
}
switch CloudKitZoneResult.resolve(error) { switch CloudKitZoneResult.resolve(error) {
case .success: case .success:
@ -497,6 +531,7 @@ extension CloudKitZone {
completion(.failure(CloudKitZoneError.userDeletedZone)) completion(.failure(CloudKitZoneError.userDeletedZone))
} }
case .retry(let timeToWait): case .retry(let timeToWait):
os_log(.error, log: self.log, "%@ zone fetch changes retry in %@ seconds.", Self.zoneID.zoneName, timeToWait)
self.retryIfPossible(after: timeToWait) { self.retryIfPossible(after: timeToWait) {
self.fetchChangesInZone(completion: completion) self.fetchChangesInZone(completion: completion)
} }