Delete completion-based selectPendingReadStatusArticleIDs method.

This commit is contained in:
Brent Simmons 2024-03-25 22:07:21 -07:00
parent 951349ffc5
commit 8b84ed6e2f
9 changed files with 87 additions and 140 deletions

View File

@ -415,13 +415,11 @@ enum CloudKitAccountDelegateError: LocalizedError {
}
self.database.insertStatuses(syncStatuses) { _ in
self.database.selectPendingCount { result in
MainActor.assumeIsolated {
if let count = try? result.get(), count > 100 {
self.sendArticleStatus(for: account, showProgress: false) { _ in }
}
completion(.success(()))
Task { @MainActor in
if let count = try? await self.database.selectPendingCount(), count > 100 {
self.sendArticleStatus(for: account, showProgress: false) { _ in }
}
completion(.success(()))
}
}
case .failure(let error):

View File

@ -34,31 +34,23 @@ class CloudKitArticlesZoneDelegate: CloudKitZoneDelegate {
func cloudKitDidModify(changed: [CKRecord], deleted: [CloudKitRecordKey], completion: @escaping (Result<Void, Error>) -> Void) {
database.selectPendingReadStatusArticleIDs() { result in
switch result {
case .success(let pendingReadStatusArticleIDs):
Task { @MainActor in
do {
Task { @MainActor in
let pendingReadStatusArticleIDs = (try await self.database.selectPendingReadStatusArticleIDs()) ?? Set<String>()
let pendingStarredStatusArticleIDs = (try await self.database.selectPendingStarredStatusArticleIDs()) ?? Set<String>()
do {
let pendingStarredStatusArticleIDs = (try await self.database.selectPendingStarredStatusArticleIDs()) ?? Set<String>()
self.delete(recordKeys: deleted, pendingStarredStatusArticleIDs: pendingStarredStatusArticleIDs) {
Task { @MainActor in
self.update(records: changed,
pendingReadStatusArticleIDs: pendingReadStatusArticleIDs,
pendingStarredStatusArticleIDs: pendingStarredStatusArticleIDs,
completion: completion)
}
}
} catch {
os_log(.error, log: self.log, "Error occurred getting pending starred records: %@", error.localizedDescription)
completion(.failure(CloudKitZoneError.unknown))
self.delete(recordKeys: deleted, pendingStarredStatusArticleIDs: pendingStarredStatusArticleIDs) {
Task { @MainActor in
self.update(records: changed,
pendingReadStatusArticleIDs: pendingReadStatusArticleIDs,
pendingStarredStatusArticleIDs: pendingStarredStatusArticleIDs,
completion: completion)
}
}
case .failure(let error):
os_log(.error, log: self.log, "Error occurred getting pending read status records: %@", error.localizedDescription)
} catch {
os_log(.error, log: self.log, "Error occurred getting pending status records: %@", error.localizedDescription)
completion(.failure(CloudKitZoneError.unknown))
}
}

View File

@ -46,28 +46,24 @@ class CloudKitSendStatusOperation: MainThreadOperation {
if showProgress {
database.selectPendingCount() { result in
MainActor.assumeIsolated {
switch result {
case .success(let count):
let ticks = count / self.blockSize
self.refreshProgress?.addToNumberOfTasksAndRemaining(ticks)
self.selectForProcessing()
case .failure(let databaseError):
os_log(.error, log: self.log, "Send status count pending error: %@.", databaseError.localizedDescription)
self.operationDelegate?.cancelOperation(self)
}
Task { @MainActor in
do {
let count = (try await self.database.selectPendingCount()) ?? 0
let ticks = count / self.blockSize
self.refreshProgress?.addToNumberOfTasksAndRemaining(ticks)
self.selectForProcessing()
} catch {
os_log(.error, log: self.log, "Send status count pending error: %@.", error.localizedDescription)
self.operationDelegate?.cancelOperation(self)
}
}
} else {
selectForProcessing()
}
}
}
private extension CloudKitSendStatusOperation {

View File

@ -565,13 +565,12 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
self.database.insertStatuses(syncStatuses) { _ in
self.database.selectPendingCount { result in
MainActor.assumeIsolated {
if let count = try? result.get(), count > 100 {
self.sendArticleStatus(for: account) { _ in }
}
completion(.success(()))
Task { @MainActor in
if let count = try? await self.database.selectPendingCount(), count > 100 {
self.sendArticleStatus(for: account) { _ in }
}
completion(.success(()))
}
}
case .failure(let error):
@ -1304,9 +1303,10 @@ private extension FeedbinAccountDelegate {
return
}
database.selectPendingReadStatusArticleIDs() { result in
Task { @MainActor in
do {
MainActor.assumeIsolated {
let pendingArticleIDs = (try await self.database.selectPendingReadStatusArticleIDs()) ?? Set<String>()
@MainActor func process(_ pendingArticleIDs: Set<String>) {
@ -1340,18 +1340,14 @@ private extension FeedbinAccountDelegate {
}
}
}
}
switch result {
case .success(let pendingArticleIDs):
process(pendingArticleIDs)
case .failure(let error):
os_log(.error, log: self.log, "Sync Article Read Status failed: %@.", error.localizedDescription)
}
process(pendingArticleIDs)
} catch {
os_log(.error, log: self.log, "Sync Article Read Status failed: %@.", error.localizedDescription)
}
}
}
func syncArticleStarredState(account: Account, articleIDs: [Int]?, completion: @escaping (() -> Void)) {

View File

@ -515,13 +515,12 @@ final class FeedlyAccountDelegate: AccountDelegate {
}
self.database.insertStatuses(syncStatuses) { _ in
self.database.selectPendingCount { result in
MainActor.assumeIsolated {
if let count = try? result.get(), count > 100 {
self.sendArticleStatus(for: account) { _ in }
}
completion(.success(()))
Task { @MainActor in
if let count = try? await self.database.selectPendingCount(), count > 100 {
self.sendArticleStatus(for: account) { _ in }
}
completion(.success(()))
}
}
case .failure(let error):

View File

@ -324,13 +324,16 @@ extension NewsBlurAccountDelegate {
}
func syncStoryReadState(account: Account, hashes: [NewsBlurStoryHash]?, completion: @escaping (() -> Void)) {
guard let hashes = hashes else {
completion()
return
}
database.selectPendingReadStatusArticleIDs() { result in
MainActor.assumeIsolated {
Task { @MainActor in
do {
let pendingArticleIDs = (try await self.database.selectPendingReadStatusArticleIDs()) ?? Set<String>()
@MainActor func process(_ pendingStoryHashes: Set<String>) {
let newsBlurUnreadStoryHashes = Set(hashes.map { $0.hash } )
@ -365,12 +368,9 @@ extension NewsBlurAccountDelegate {
}
}
switch result {
case .success(let pendingArticleIDs):
process(pendingArticleIDs)
case .failure(let error):
os_log(.error, log: self.log, "Sync Story Read Status failed: %@.", error.localizedDescription)
}
process(pendingArticleIDs)
} catch {
os_log(.error, log: self.log, "Sync Story Read Status failed: %@.", error.localizedDescription)
}
}
}

View File

@ -595,13 +595,12 @@ final class NewsBlurAccountDelegate: AccountDelegate {
}
self.database.insertStatuses(syncStatuses) { _ in
self.database.selectPendingCount { result in
MainActor.assumeIsolated {
if let count = try? result.get(), count > 100 {
self.sendArticleStatus(for: account) { _ in }
}
completion(.success(()))
Task { @MainActor in
if let count = try? await self.database.selectPendingCount(), count > 100 {
self.sendArticleStatus(for: account) { _ in }
}
completion(.success(()))
}
}
case .failure(let error):

View File

@ -621,13 +621,12 @@ final class ReaderAPIAccountDelegate: AccountDelegate {
}
self.database.insertStatuses(syncStatuses) { _ in
self.database.selectPendingCount { result in
MainActor.assumeIsolated {
if let count = try? result.get(), count > 100 {
self.sendArticleStatus(for: account) { _ in }
}
completion(.success(()))
Task { @MainActor in
if let count = try? await self.database.selectPendingCount(), count > 100 {
self.sendArticleStatus(for: account) { _ in }
}
completion(.success(()))
}
}
case .failure(let error):
@ -1108,9 +1107,11 @@ private extension ReaderAPIAccountDelegate {
return
}
database.selectPendingReadStatusArticleIDs() { result in
Task { @MainActor in
do {
let pendingArticleIDs = (try await self.database.selectPendingReadStatusArticleIDs()) ?? Set<String>()
MainActor.assumeIsolated {
@MainActor func process(_ pendingArticleIDs: Set<String>) {
let updatableReaderUnreadArticleIDs = Set(articleIDs).subtracting(pendingArticleIDs)
@ -1144,15 +1145,12 @@ private extension ReaderAPIAccountDelegate {
}
}
switch result {
case .success(let pendingArticleIDs):
process(pendingArticleIDs)
case .failure(let error):
os_log(.error, log: self.log, "Sync Article Read Status failed: %@.", error.localizedDescription)
}
process(pendingArticleIDs)
} catch {
os_log(.error, log: self.log, "Sync Article Read Status failed: %@.", error.localizedDescription)
}
}
}
func syncArticleStarredState(account: Account, articleIDs: [String]?, completion: @escaping (() -> Void)) {

View File

@ -148,37 +148,6 @@ public extension SyncDatabase {
}
}
}
nonisolated func selectPendingCount(completion: @escaping DatabaseIntCompletionBlock) {
Task { @MainActor in
do {
if let count = try await self.selectPendingCount() {
completion(.success(count))
} else {
completion(.success(0))
}
} catch {
completion(.failure(DatabaseError.suspended))
}
}
}
nonisolated func selectPendingReadStatusArticleIDs(completion: @escaping SyncStatusArticleIDsCompletionBlock) {
Task { @MainActor in
do {
if let articleIDs = try await self.selectPendingReadStatusArticleIDs() {
completion(.success(articleIDs))
} else {
completion(.success(Set<String>()))
}
} catch {
completion(.failure(DatabaseError.suspended))
}
}
}
}
private extension SyncDatabase {