Skip to content

Commit

Permalink
SwiftUI minor performance boost for huge lists
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoarment committed May 1, 2023
1 parent f44027f commit b15ce1a
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions Sources/Blackbird/BlackbirdSwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,27 @@ public final class BlackbirdModelInstanceChangeObserver<T: BlackbirdModel> {
guard let database, database != currentDatabase else { return }
currentDatabase = database
cachedInstance.value = nil

let primaryKeyValues = primaryKeyValues
changeObserver.value = T.changePublisher(in: database, multicolumnPrimaryKey: primaryKeyValues)
.sink { _ in
Task.detached { [weak self] in
self?.cachedInstance.value = nil
await self?.update()
}
}

Task.detached { [weak self] in await self?.update() }
if updatesEnabled {
Task.detached { [weak self] in await self?.update() }
}
}

public func update() async {
guard let currentDatabase, updatesEnabled else { return }

changeObserver.withLock { observer in
if observer != nil { return }

let primaryKeyValues = primaryKeyValues
observer = T.changePublisher(in: currentDatabase, multicolumnPrimaryKey: primaryKeyValues)
.sink { _ in
Task.detached { [weak self] in
self?.cachedInstance.value = nil
await self?.update()
}
}
}

if let cachedInstance = cachedInstance.value {
currentInstance = cachedInstance
Expand Down

0 comments on commit b15ce1a

Please sign in to comment.