diff --git a/Riot/Managers/PushRulesUpdater/PushRulesUpdater.swift b/Riot/Managers/PushRulesUpdater/PushRulesUpdater.swift index 90bd221ad9..50620ecaf5 100644 --- a/Riot/Managers/PushRulesUpdater/PushRulesUpdater.swift +++ b/Riot/Managers/PushRulesUpdater/PushRulesUpdater.swift @@ -44,35 +44,33 @@ final class PushRulesUpdater { private extension PushRulesUpdater { func syncRulesIfNeeded() { - let dispatchGroup: DispatchGroup = .init() - - for rule in rules { - guard let ruleId = rule.pushRuleId else { - continue + Task { + await withTaskGroup(of: Void.self) { [rules, notificationSettingsService] group in + for rule in rules { + guard let ruleId = rule.pushRuleId else { + continue + } + + let relatedRules = ruleId.syncedRules(in: rules) + + for relatedRule in relatedRules { + guard rule.hasSameContentOf(relatedRule) == false else { + continue + } + + group.addTask { + try? await notificationSettingsService.updatePushRuleActions(for: relatedRule.ruleId, + enabled: rule.enabled, + actions: rule.ruleActions) + } + } + } } - let relatedRules = ruleId.syncedRules(in: rules) - - for relatedRule in relatedRules { - guard rule.hasSameContentOf(relatedRule) == false else { - continue - } - - dispatchGroup.enter() - Task { - try? await sync(relatedRuleId: relatedRule.ruleId, with: rule) - dispatchGroup.leave() - } + await MainActor.run { [weak self] in + self?.didCompleteUpdateSubject.send(()) } } - - dispatchGroup.notify(queue: .main) { [weak self] in - self?.didCompleteUpdateSubject.send(()) - } - } - - func sync(relatedRuleId: String, with rule: NotificationPushRuleType) async throws { - try await notificationSettingsService.updatePushRuleActions(for: relatedRuleId, enabled: rule.enabled, actions: rule.ruleActions) } } diff --git a/RiotSwiftUI/Modules/Settings/Notifications/Service/MatrixSDK/MXNotificationSettingsService.swift b/RiotSwiftUI/Modules/Settings/Notifications/Service/MatrixSDK/MXNotificationSettingsService.swift index 6ccd54c1a4..9bf01ef4c0 100644 --- a/RiotSwiftUI/Modules/Settings/Notifications/Service/MatrixSDK/MXNotificationSettingsService.swift +++ b/RiotSwiftUI/Modules/Settings/Notifications/Service/MatrixSDK/MXNotificationSettingsService.swift @@ -89,10 +89,10 @@ class MXNotificationSettingsService: NotificationSettingsServiceType { // Updating the actions before enabling the rule allows the homeserver to triggers just one sync update try await session.notificationCenter.updatePushRuleActions(ruleId, - kind: rule.kind, - notify: actions.notify, - soundName: actions.sound, - highlight: actions.highlight) + kind: rule.kind, + notify: actions.notify, + soundName: actions.sound, + highlight: actions.highlight) try await session.notificationCenter.enableRule(pushRule: rule, isEnabled: enabled) }