From 22c2a7a72e8b5630b89f7552f055fb539fc89aa1 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 12 Apr 2020 16:36:35 -0700 Subject: [PATCH] =?UTF-8?q?Delete=20old=20articles=20in=20a=20graduated=20?= =?UTF-8?q?way,=20so=20as=20not=20to=20block=20the=20database=20for=20too?= =?UTF-8?q?=20long.=20Also:=20don=E2=80=98t=20delete=20old=20unread=20arti?= =?UTF-8?q?cles=20in=20accounts=20that=20use=20an=20external=20sync=20serv?= =?UTF-8?q?ice.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArticlesDatabase/ArticlesTable.swift | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) 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) } }