make sure coredata uses the right thread

This commit is contained in:
Alek Michelson 2026-04-08 19:54:39 -04:00
parent 3feb27848a
commit 8714009cfa
2 changed files with 34 additions and 33 deletions

View file

@ -101,8 +101,10 @@ class Store: ObservableObject {
} }
func delete(subscription: Subscription) { func delete(subscription: Subscription) {
context.delete(subscription) context.performAndWait {
try? context.save() context.delete(subscription)
try? context.save()
}
} }
// MARK: Notifications // MARK: Notifications
@ -130,35 +132,41 @@ class Store: ObservableObject {
} }
func delete(notification: Notification) { func delete(notification: Notification) {
Log.d(Store.tag, "Deleting notification \(notification.id ?? "")") context.performAndWait {
context.delete(notification) Log.d(Store.tag, "Deleting notification \(notification.id ?? "")")
try? context.save() context.delete(notification)
try? context.save()
}
} }
func delete(notifications: Set<Notification>) { func delete(notifications: Set<Notification>) {
Log.d(Store.tag, "Deleting \(notifications.count) notification(s)") context.performAndWait {
do { Log.d(Store.tag, "Deleting \(notifications.count) notification(s)")
notifications.forEach { notification in do {
context.delete(notification) 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) { func delete(allNotificationsFor subscription: Subscription) {
guard let notifications = subscription.notifications else { return } context.performAndWait {
Log.d(Store.tag, "Deleting all \(notifications.count) notification(s) for subscription \(subscription.urlString())") guard let notifications = subscription.notifications else { return }
do { Log.d(Store.tag, "Deleting all \(notifications.count) notification(s) for subscription \(subscription.urlString())")
notifications.forEach { notification in do {
context.delete(notification as! Notification) 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()
} }
} }

View file

@ -206,23 +206,17 @@ struct NotificationListView: View {
} }
private func unsubscribe() { private func unsubscribe() {
DispatchQueue.global(qos: .background).async { subscriptionManager.unsubscribe(subscription)
subscriptionManager.unsubscribe(subscription)
}
delegate.selectedBaseUrl = nil delegate.selectedBaseUrl = nil
} }
private func deleteAll() { private func deleteAll() {
DispatchQueue.global(qos: .background).async { store.delete(allNotificationsFor: subscription)
store.delete(allNotificationsFor: subscription)
}
} }
private func deleteSelected() { private func deleteSelected() {
DispatchQueue.global(qos: .background).async { store.delete(notifications: selection)
store.delete(notifications: selection) selection = Set<Notification>()
selection = Set<Notification>()
}
editMode = .inactive editMode = .inactive
} }
@ -346,4 +340,3 @@ struct NotificationListView_Previews: PreviewProvider {
} }
} }
} }