From c9462f5a01ef4b298caea661981fa6cecff687b7 Mon Sep 17 00:00:00 2001 From: Dominik Kapusta Date: Mon, 15 Jul 2024 06:59:43 +0200 Subject: [PATCH] Add a debug menu action to reset Remote Messages on macOS (#891) Task/Issue URL: https://app.asana.com/0/1199230911884351/1207797025533577/f Description: This change adds API to reset Remote Messages by deleting all records in the database. --- .../RemoteMessagingStore.swift | 21 +++++++++++++++++++ .../RemoteMessagingStoring.swift | 6 +++++- .../MockRemoteMessagingStore.swift | 2 ++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Sources/RemoteMessaging/RemoteMessagingStore.swift b/Sources/RemoteMessaging/RemoteMessagingStore.swift index 615e6dc18..83b46cb4a 100644 --- a/Sources/RemoteMessaging/RemoteMessagingStore.swift +++ b/Sources/RemoteMessaging/RemoteMessagingStore.swift @@ -360,6 +360,27 @@ extension RemoteMessagingStore { } } } + + public func resetRemoteMessages() { + guard remoteMessagingAvailabilityProvider.isRemoteMessagingAvailable else { + return + } + + let context = database.makeContext(concurrencyType: .privateQueueConcurrencyType, name: Constants.privateContextName) + context.performAndWait { + context.deleteAll(entityDescriptions: [ + RemoteMessageManagedObject.entity(in: context), + RemoteMessagingConfigManagedObject.entity(in: context) + ]) + + do { + try context.save() + } catch { + os_log("Failed to reset remote messages", log: log, type: .error) + } + } + notificationCenter.post(name: Notifications.remoteMessagesDidChange, object: nil) + } } // MARK: - RemoteMessageManagedObject Private Interface diff --git a/Sources/RemoteMessaging/RemoteMessagingStoring.swift b/Sources/RemoteMessaging/RemoteMessagingStoring.swift index 321d20bbe..c996114a9 100644 --- a/Sources/RemoteMessaging/RemoteMessagingStoring.swift +++ b/Sources/RemoteMessaging/RemoteMessagingStoring.swift @@ -18,7 +18,11 @@ import Foundation -public protocol RemoteMessagingStoring { +public protocol RemoteMessagingStoringDebuggingSupport { + func resetRemoteMessages() +} + +public protocol RemoteMessagingStoring: RemoteMessagingStoringDebuggingSupport { func saveProcessedResult(_ processorResult: RemoteMessagingConfigProcessor.ProcessorResult) func fetchRemoteMessagingConfig() -> RemoteMessagingConfig? diff --git a/Sources/RemoteMessagingTestsUtils/MockRemoteMessagingStore.swift b/Sources/RemoteMessagingTestsUtils/MockRemoteMessagingStore.swift index bbdb5e92a..c12723d2c 100644 --- a/Sources/RemoteMessagingTestsUtils/MockRemoteMessagingStore.swift +++ b/Sources/RemoteMessagingTestsUtils/MockRemoteMessagingStore.swift @@ -92,4 +92,6 @@ public class MockRemoteMessagingStore: RemoteMessagingStoring { public func updateRemoteMessage(withID id: String, asShown shown: Bool) { updateRemoteMessageCalls += 1 } + + public func resetRemoteMessages() {} }