diff --git a/Frameworks/ArticlesDatabase/ArticlesTable.swift b/Frameworks/ArticlesDatabase/ArticlesTable.swift index 9aa7bc06b..796731df8 100644 --- a/Frameworks/ArticlesDatabase/ArticlesTable.swift +++ b/Frameworks/ArticlesDatabase/ArticlesTable.swift @@ -488,20 +488,41 @@ final class ArticlesTable: DatabaseTable { // MARK: - Cleanup /// Delete articles that we won’t show in the UI any longer - /// — their arrival date is before our 90-day recency window. - /// Keep all starred articles, no matter their age. + /// — their arrival date is before our 90-day recency window; + /// they are read; they are not starred. + /// + /// Because deleting articles might block the database for too long, + /// we do this in a careful way: delete articles older than a year, + /// check to see how much time has passed, then decide whether or not to continue. + /// Repeat for successively shorter time intervals. func deleteOldArticles() { - queue.runInTransaction { databaseResult in + precondition(retentionStyle == .syncSystem) - func makeDatabaseCalls(_ database: FMDatabase) { - let sql = "delete from articles where articleID in (select articleID from articles natural join statuses where dateArrived Bool { + let timeElapsed = Date().timeIntervalSince(startTime) + return timeElapsed > 2.0 } + + let dayIntervals = [365, 300, 225, 150] + for dayInterval in dayIntervals { + deleteOldArticles(cutoffDate: startTime.bySubtracting(days: dayInterval)) + if tooMuchTimeHasPassed() { + return + } + } + deleteOldArticles(cutoffDate: self.articleCutoffDate) } }