From 27fff58fea4d1b78a503813118aa2a656af600e3 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Sun, 20 Nov 2022 17:25:04 -0800 Subject: [PATCH] Do not clean on main thread --- DB/Sources/DB/Content/ContentDatabase.swift | 51 +++++++++---------- .../Services/TimelineService.swift | 11 +++- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/DB/Sources/DB/Content/ContentDatabase.swift b/DB/Sources/DB/Content/ContentDatabase.swift index 8dfe741..05a47ff 100644 --- a/DB/Sources/DB/Content/ContentDatabase.swift +++ b/DB/Sources/DB/Content/ContentDatabase.swift @@ -12,6 +12,7 @@ public struct ContentDatabase { private let id: Identity.Id private let databaseWriter: DatabaseWriter + private let useHomeTimelineLastReadId: Bool public init(id: Identity.Id, useHomeTimelineLastReadId: Bool, @@ -19,6 +20,7 @@ public struct ContentDatabase { appGroup: String, keychain: Keychain.Type) throws { self.id = id + self.useHomeTimelineLastReadId = useHomeTimelineLastReadId if inMemory { databaseWriter = try DatabaseQueue() @@ -31,10 +33,6 @@ public struct ContentDatabase { } } - try Self.clean( - databaseWriter, - useHomeTimelineLastReadId: useHomeTimelineLastReadId) - activeFiltersPublisher = ValueObservation.tracking { try Filter.filter(Filter.Columns.expiresAt == nil || Filter.Columns.expiresAt > Date()).fetchAll($0) } @@ -121,6 +119,28 @@ public extension ContentDatabase { } } + func cleanHomeTimelinePublisher() -> AnyPublisher { + databaseWriter.mutatingPublisher { + try NotificationRecord.deleteAll($0) + try ConversationRecord.deleteAll($0) + try StatusAncestorJoin.deleteAll($0) + try StatusDescendantJoin.deleteAll($0) + try AccountList.deleteAll($0) + + if useHomeTimelineLastReadId { + try TimelineRecord.filter(TimelineRecord.Columns.id != Timeline.home.id).deleteAll($0) + try StatusRecord.filter(Self.statusIdsToDeleteForPositionPreservingClean(db: $0) + .contains(StatusRecord.Columns.id)).deleteAll($0) + try AccountRecord.filter(Self.accountIdsToDeleteForPositionPreservingClean(db: $0) + .contains(AccountRecord.Columns.id)).deleteAll($0) + } else { + try TimelineRecord.deleteAll($0) + try StatusRecord.deleteAll($0) + try AccountRecord.deleteAll($0) + } + } + } + func insert(context: Context, parentId: Status.Id) -> AnyPublisher { databaseWriter.mutatingPublisher { for (index, status) in context.ancestors.enumerated() { @@ -698,27 +718,4 @@ private extension ContentDatabase { return Set(Array(staleAccountIds).prefix(Self.cleanLimit)) } - - static func clean(_ databaseWriter: DatabaseWriter, - useHomeTimelineLastReadId: Bool) throws { - try databaseWriter.write { - try NotificationRecord.deleteAll($0) - try ConversationRecord.deleteAll($0) - try StatusAncestorJoin.deleteAll($0) - try StatusDescendantJoin.deleteAll($0) - try AccountList.deleteAll($0) - - if useHomeTimelineLastReadId { - try TimelineRecord.filter(TimelineRecord.Columns.id != Timeline.home.id).deleteAll($0) - try StatusRecord.filter(statusIdsToDeleteForPositionPreservingClean(db: $0) - .contains(StatusRecord.Columns.id)).deleteAll($0) - try AccountRecord.filter(accountIdsToDeleteForPositionPreservingClean(db: $0) - .contains(AccountRecord.Columns.id)).deleteAll($0) - } else { - try TimelineRecord.deleteAll($0) - try StatusRecord.deleteAll($0) - try AccountRecord.deleteAll($0) - } - } - } } diff --git a/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift b/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift index 68b890e..fe01c8d 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/TimelineService.swift @@ -28,7 +28,16 @@ public struct TimelineService { self.timeline = timeline self.mastodonAPIClient = mastodonAPIClient self.contentDatabase = contentDatabase - sections = contentDatabase.timelinePublisher(timeline) + + if case .home = timeline { + sections = contentDatabase.cleanHomeTimelinePublisher() + .collect() + .flatMap { _ in contentDatabase.timelinePublisher(timeline) } + .eraseToAnyPublisher() + } else { + sections = contentDatabase.timelinePublisher(timeline) + } + navigationService = NavigationService(environment: environment, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)