diff --git a/Mac/MainWindow/AddFeed/AddFeedWindowController.swift b/Mac/MainWindow/AddFeed/AddFeedWindowController.swift index cbfa2d890..2f571fa63 100644 --- a/Mac/MainWindow/AddFeed/AddFeedWindowController.swift +++ b/Mac/MainWindow/AddFeed/AddFeedWindowController.swift @@ -137,7 +137,9 @@ private extension AddFeedWindowController { } func selectedContainer() -> Container? { - guard folderPopupButton.selectedItem?.isEnabled ?? false else { return nil } - return folderPopupButton.selectedItem?.representedObject as? Container + guard let selectedItem = folderPopupButton.selectedItem, selectedItem.isEnabled else { + return nil + } + return selectedItem.representedObject as? Container } } diff --git a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift index 96a03c4c9..d6862b150 100644 --- a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift @@ -449,6 +449,11 @@ public enum FeedbinAccountDelegateError: String, Error, Sendable { func accountDidInitialize() { credentials = try? account?.retrieveCredentials(type: .basic) + + // A send in progress when the app was killed left its statuses selected. Clear them so + // they get sent, instead of waiting for the next selectForProcessing to pick them up. + // + syncDatabase.resetAllSelectedForProcessing() } func accountWillBeDeleted() { diff --git a/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift b/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift index 16d0b06b5..bcb631eee 100644 --- a/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift +++ b/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift @@ -779,6 +779,11 @@ import Secrets } Self.logger.debug("FeedlyAccountDelegate: accountDidInitialize") credentials = try? account.retrieveCredentials(type: .oauthAccessToken) + + // A send in progress when the app was killed left its statuses selected. Clear them so + // they get sent, instead of waiting for the next selectForProcessing to pick them up. + // + syncDatabase.resetAllSelectedForProcessing() } func accountWillBeDeleted() { diff --git a/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift b/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift index 10cfedb74..7c908e6a0 100644 --- a/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift +++ b/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift @@ -511,6 +511,11 @@ import Secrets func accountDidInitialize() { credentials = try? account?.retrieveCredentials(type: .newsBlurSessionID) + + // A send in progress when the app was killed left its statuses selected. Clear them so + // they get sent, instead of waiting for the next selectForProcessing to pick them up. + // + syncDatabase.resetAllSelectedForProcessing() } func accountWillBeDeleted() { diff --git a/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift b/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift index 651b3472b..fbb8d76a1 100644 --- a/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift +++ b/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift @@ -679,6 +679,11 @@ final class ReaderAPIAccountDelegate: AccountDelegate { return } retrieveCredentialsIfNeeded(account) + + // A send in progress when the app was killed left its statuses selected. Clear them so + // they get sent, instead of waiting for the next selectForProcessing to pick them up. + // + syncDatabase.resetAllSelectedForProcessing() } func accountWillBeDeleted() { diff --git a/Modules/SyncDatabase/Sources/SyncDatabase/SyncStatusTable.swift b/Modules/SyncDatabase/Sources/SyncDatabase/SyncStatusTable.swift index 3620dab62..f7d4bb117 100644 --- a/Modules/SyncDatabase/Sources/SyncDatabase/SyncStatusTable.swift +++ b/Modules/SyncDatabase/Sources/SyncDatabase/SyncStatusTable.swift @@ -14,17 +14,14 @@ import RSDatabaseObjC struct SyncStatusTable { static let name = "syncStatus" + // Selects first, then marks just the rows being returned. Marking every row would flag rows + // past the limit as in-flight, and `deleteSelectedForProcessing` with a nil key would then + // delete a queued status that was never sent. static func selectForProcessing(limit: Int?, database: FMDatabase) -> Set? { database.beginTransaction() - let updateSQL = "update \(name) set selected = true" - guard database.executeUpdate(updateSQL, withArgumentsIn: nil) else { - database.rollback() - return nil - } - let selectSQL = { - var sql = "select * from \(name) where selected == true" + var sql = "select * from \(name)" if let limit { sql = "\(sql) limit \(limit)" } @@ -37,10 +34,28 @@ struct SyncStatusTable { } let statuses = resultSet.mapToSet(statusWithRow) + guard !statuses.isEmpty else { + database.commit() + return statuses + } + + // An articleID can have both a read row and a starred row, so match on the pair. + var parameters = [AnyObject]() + let conditions = statuses.map { status -> String in + parameters.append(status.articleID as AnyObject) + parameters.append(status.key.rawValue as AnyObject) + return "(articleID = ? and key = ?)" + } + + let updateSQL = "update \(name) set selected = true where \(conditions.joined(separator: " or "))" + guard database.executeUpdate(updateSQL, withArgumentsIn: parameters) else { + database.rollback() + return nil + } database.commit() - return statuses + return Set(statuses.map { SyncStatus(articleID: $0.articleID, key: $0.key, flag: $0.flag, selected: true) }) } static func selectPendingCount(database: FMDatabase) -> Int? { @@ -124,10 +139,10 @@ private extension SyncStatusTable { return SyncStatus(articleID: articleID, key: key, flag: flag, selected: selected) } + // Includes rows that are selected — a send in flight hasn’t been confirmed by the server yet, + // and skipping those let a concurrent refresh revert the change the send was carrying. + // static func selectPendingArticleIDs(_ statusKey: ArticleStatus.Key, database: FMDatabase) -> Set? { - // Rows selected for an in-flight send are still pending — they’re only deleted - // once the send is confirmed. Excluding them let a concurrent send make every - // pending edit invisible to status ingestion, which then reverted the edits. let sql = "select articleID from \(name) where key = \"\(statusKey.rawValue)\";" guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else { return nil