From d97e404cc790a60a5a2b9294b073de91b9baea4b Mon Sep 17 00:00:00 2001 From: Ilyas Hallak Date: Sun, 19 Oct 2025 20:40:02 +0200 Subject: [PATCH] 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. --- readeck/UI/Bookmarks/BookmarksViewModel.swift | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/readeck/UI/Bookmarks/BookmarksViewModel.swift b/readeck/UI/Bookmarks/BookmarksViewModel.swift index b4e43e8..f2c569f 100644 --- a/readeck/UI/Bookmarks/BookmarksViewModel.swift +++ b/readeck/UI/Bookmarks/BookmarksViewModel.swift @@ -22,9 +22,12 @@ class BookmarksViewModel { var showingAddBookmarkFromShare = false var shareURL = "" var shareTitle = "" - + // Undo delete functionality var pendingDeletes: [String: PendingDelete] = [:] // bookmarkId -> PendingDelete + + // Prevent concurrent updates + private var isUpdating = false private var cancellables = Set() @@ -104,15 +107,19 @@ 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 currentType = type currentTag = tag - + offset = 0 hasMoreData = true - + do { let newBookmarks = try await getBooksmarksUseCase.execute( state: state, @@ -142,18 +149,20 @@ class BookmarksViewModel { } // Don't clear bookmarks on error - keep existing data visible } - + isLoading = false isInitialLoading = false } @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 - + do { offset += limit // inc. offset let newBookmarks = try await getBooksmarksUseCase.execute( @@ -181,7 +190,7 @@ class BookmarksViewModel { errorMessage = "Error loading more bookmarks" } } - + isLoading = false }