From 8714009cfa17ba3d77ebf81daf7ab9862c6f90b3 Mon Sep 17 00:00:00 2001 From: Alek Michelson Date: Wed, 8 Apr 2026 19:54:39 -0400 Subject: [PATCH] make sure coredata uses the right thread --- ntfy/Persistence/Store.swift | 52 +++++++++++++++------------ ntfy/Views/NotificationListView.swift | 15 +++----- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/ntfy/Persistence/Store.swift b/ntfy/Persistence/Store.swift index c01fe8e..955693e 100644 --- a/ntfy/Persistence/Store.swift +++ b/ntfy/Persistence/Store.swift @@ -101,8 +101,10 @@ class Store: ObservableObject { } func delete(subscription: Subscription) { - context.delete(subscription) - try? context.save() + context.performAndWait { + context.delete(subscription) + try? context.save() + } } // MARK: Notifications @@ -130,35 +132,41 @@ class Store: ObservableObject { } func delete(notification: Notification) { - Log.d(Store.tag, "Deleting notification \(notification.id ?? "")") - context.delete(notification) - try? context.save() + context.performAndWait { + Log.d(Store.tag, "Deleting notification \(notification.id ?? "")") + context.delete(notification) + try? context.save() + } } func delete(notifications: Set) { - Log.d(Store.tag, "Deleting \(notifications.count) notification(s)") - do { - notifications.forEach { notification in - context.delete(notification) + context.performAndWait { + Log.d(Store.tag, "Deleting \(notifications.count) notification(s)") + do { + notifications.forEach { notification in + context.delete(notification) + } + try context.save() + } catch let error { + Log.w(Store.tag, "Cannot delete notification(s)", error) + rollbackAndRefresh() } - try context.save() - } catch let error { - Log.w(Store.tag, "Cannot delete notification(s)", error) - rollbackAndRefresh() } } func delete(allNotificationsFor subscription: Subscription) { - guard let notifications = subscription.notifications else { return } - Log.d(Store.tag, "Deleting all \(notifications.count) notification(s) for subscription \(subscription.urlString())") - do { - notifications.forEach { notification in - context.delete(notification as! Notification) + context.performAndWait { + guard let notifications = subscription.notifications else { return } + Log.d(Store.tag, "Deleting all \(notifications.count) notification(s) for subscription \(subscription.urlString())") + do { + notifications.forEach { notification in + context.delete(notification as! Notification) + } + try context.save() + } catch let error { + Log.w(Store.tag, "Cannot delete notification(s)", error) + rollbackAndRefresh() } - try context.save() - } catch let error { - Log.w(Store.tag, "Cannot delete notification(s)", error) - rollbackAndRefresh() } } diff --git a/ntfy/Views/NotificationListView.swift b/ntfy/Views/NotificationListView.swift index b4d94c3..ad477e6 100644 --- a/ntfy/Views/NotificationListView.swift +++ b/ntfy/Views/NotificationListView.swift @@ -206,23 +206,17 @@ struct NotificationListView: View { } private func unsubscribe() { - DispatchQueue.global(qos: .background).async { - subscriptionManager.unsubscribe(subscription) - } + subscriptionManager.unsubscribe(subscription) delegate.selectedBaseUrl = nil } private func deleteAll() { - DispatchQueue.global(qos: .background).async { - store.delete(allNotificationsFor: subscription) - } + store.delete(allNotificationsFor: subscription) } private func deleteSelected() { - DispatchQueue.global(qos: .background).async { - store.delete(notifications: selection) - selection = Set() - } + store.delete(notifications: selection) + selection = Set() editMode = .inactive } @@ -346,4 +340,3 @@ struct NotificationListView_Previews: PreviewProvider { } } } -