Do not clean on main thread

This commit is contained in:
Justin Mazzocchi 2022-11-20 17:25:04 -08:00
parent 9bfc6c1449
commit 27fff58fea
2 changed files with 34 additions and 28 deletions

View file

@ -12,6 +12,7 @@ public struct ContentDatabase {
private let id: Identity.Id private let id: Identity.Id
private let databaseWriter: DatabaseWriter private let databaseWriter: DatabaseWriter
private let useHomeTimelineLastReadId: Bool
public init(id: Identity.Id, public init(id: Identity.Id,
useHomeTimelineLastReadId: Bool, useHomeTimelineLastReadId: Bool,
@ -19,6 +20,7 @@ public struct ContentDatabase {
appGroup: String, appGroup: String,
keychain: Keychain.Type) throws { keychain: Keychain.Type) throws {
self.id = id self.id = id
self.useHomeTimelineLastReadId = useHomeTimelineLastReadId
if inMemory { if inMemory {
databaseWriter = try DatabaseQueue() databaseWriter = try DatabaseQueue()
@ -31,10 +33,6 @@ public struct ContentDatabase {
} }
} }
try Self.clean(
databaseWriter,
useHomeTimelineLastReadId: useHomeTimelineLastReadId)
activeFiltersPublisher = ValueObservation.tracking { activeFiltersPublisher = ValueObservation.tracking {
try Filter.filter(Filter.Columns.expiresAt == nil || Filter.Columns.expiresAt > Date()).fetchAll($0) try Filter.filter(Filter.Columns.expiresAt == nil || Filter.Columns.expiresAt > Date()).fetchAll($0)
} }
@ -121,6 +119,28 @@ public extension ContentDatabase {
} }
} }
func cleanHomeTimelinePublisher() -> AnyPublisher<Never, Error> {
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<Never, Error> { func insert(context: Context, parentId: Status.Id) -> AnyPublisher<Never, Error> {
databaseWriter.mutatingPublisher { databaseWriter.mutatingPublisher {
for (index, status) in context.ancestors.enumerated() { for (index, status) in context.ancestors.enumerated() {
@ -698,27 +718,4 @@ private extension ContentDatabase {
return Set(Array(staleAccountIds).prefix(Self.cleanLimit)) 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)
}
}
}
} }

View file

@ -28,7 +28,16 @@ public struct TimelineService {
self.timeline = timeline self.timeline = timeline
self.mastodonAPIClient = mastodonAPIClient self.mastodonAPIClient = mastodonAPIClient
self.contentDatabase = contentDatabase self.contentDatabase = contentDatabase
if case .home = timeline {
sections = contentDatabase.cleanHomeTimelinePublisher()
.collect()
.flatMap { _ in contentDatabase.timelinePublisher(timeline) }
.eraseToAnyPublisher()
} else {
sections = contentDatabase.timelinePublisher(timeline) sections = contentDatabase.timelinePublisher(timeline)
}
navigationService = NavigationService(environment: environment, navigationService = NavigationService(environment: environment,
mastodonAPIClient: mastodonAPIClient, mastodonAPIClient: mastodonAPIClient,
contentDatabase: contentDatabase) contentDatabase: contentDatabase)