fix: Prevent UICollectionView crash from concurrent bookmark list updates

Add isUpdating flag to prevent race conditions when updating the bookmark list.
This fixes crashes that occurred when returning to the app after adding a bookmark
via the share extension while the list was being updated.
This commit is contained in:
Ilyas Hallak 2025-10-19 20:40:02 +02:00
parent 6906509aea
commit d97e404cc7

View File

@ -26,6 +26,9 @@ class BookmarksViewModel {
// Undo delete functionality
var pendingDeletes: [String: PendingDelete] = [:] // bookmarkId -> PendingDelete
// Prevent concurrent updates
private var isUpdating = false
private var cancellables = Set<AnyCancellable>()
private var limit = 50
@ -104,6 +107,10 @@ class BookmarksViewModel {
@MainActor
func loadBookmarks(state: BookmarkState = .unread, type: [BookmarkType] = [.article], tag: String? = nil) async {
guard !isUpdating else { return }
isUpdating = true
defer { isUpdating = false }
isLoading = true
errorMessage = nil
currentState = state
@ -149,7 +156,9 @@ class BookmarksViewModel {
@MainActor
func loadMoreBookmarks() async {
guard !isLoading && hasMoreData else { return } // prevent multiple loads
guard !isLoading && hasMoreData && !isUpdating else { return } // prevent multiple loads
isUpdating = true
defer { isUpdating = false }
isLoading = true
errorMessage = nil