From 049f1943045966fff2dda5be11ea3c777cbc4d9d Mon Sep 17 00:00:00 2001 From: Alek Michelson Date: Fri, 10 Apr 2026 00:42:08 -0400 Subject: [PATCH] forgot to batch --- ntfy/Persistence/Store.swift | 36 +++++++++++++--------- ntfy/Persistence/SubscriptionManager.swift | 4 +-- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/ntfy/Persistence/Store.swift b/ntfy/Persistence/Store.swift index 9cc2c90..d8bfbd0 100644 --- a/ntfy/Persistence/Store.swift +++ b/ntfy/Persistence/Store.swift @@ -115,24 +115,32 @@ class Store: ObservableObject { // MARK: Notifications func save(notificationFromMessage message: Message, withSubscription subscription: Subscription) { + save(notificationsFromMessages: [message], withSubscription: subscription) + } + + func save(notificationsFromMessages messages: [Message], withSubscription subscription: Subscription) { + guard !messages.isEmpty else { return } + context.performAndWait { do { - let notification = Notification(context: context) - notification.id = message.id - notification.time = message.time - notification.message = message.message ?? "" - notification.title = message.title ?? "" - notification.priority = (message.priority != nil && message.priority != 0) ? message.priority! : 3 - notification.tags = message.tags?.joined(separator: ",") ?? "" - notification.actions = Actions.shared.encode(message.actions) - notification.click = message.click ?? "" - notification.subscription = subscription - subscription.addToNotifications(notification) - subscription.lastNotificationId = message.id - Log.d(Store.tag, "Storing notification with ID \(notification.id ?? "")") + for message in messages { + let notification = Notification(context: context) + notification.id = message.id + notification.time = message.time + notification.message = message.message ?? "" + notification.title = message.title ?? "" + notification.priority = (message.priority != nil && message.priority != 0) ? message.priority! : 3 + notification.tags = message.tags?.joined(separator: ",") ?? "" + notification.actions = Actions.shared.encode(message.actions) + notification.click = message.click ?? "" + notification.subscription = subscription + subscription.addToNotifications(notification) + subscription.lastNotificationId = message.id + Log.d(Store.tag, "Storing notification with ID \(notification.id ?? "")") + } try context.save() } catch let error { - Log.w(Store.tag, "Cannot store notification (fromMessage)", error) + Log.w(Store.tag, "Cannot store notifications (fromMessages)", error) rollbackAndRefresh() } } diff --git a/ntfy/Persistence/SubscriptionManager.swift b/ntfy/Persistence/SubscriptionManager.swift index 8a1ab76..dfd9e70 100644 --- a/ntfy/Persistence/SubscriptionManager.swift +++ b/ntfy/Persistence/SubscriptionManager.swift @@ -61,9 +61,7 @@ struct SubscriptionManager { } Log.d(tag, "Polling success, \(messages.count) new message(s)", messages) if !messages.isEmpty { - for message in messages { - store.save(notificationFromMessage: message, withSubscription: subscription) - } + store.save(notificationsFromMessages: messages, withSubscription: subscription) } completionHandler(messages) }