Skip to content

Commit

Permalink
DispatchGroup to TaskGroup refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
alfogrillo committed Feb 2, 2023
1 parent a2b1177 commit 1c4f000
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions Riot/Managers/PushRulesUpdater/PushRulesUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,31 @@ 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] 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 { [weak self] in
try? await self?.sync(relatedRuleId: relatedRule.ruleId, with: rule)
}
}
}
}

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 {
Expand Down

0 comments on commit 1c4f000

Please sign in to comment.