From 74b6adb2477fc77c742a7ecc13ec7588032846f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20F=C3=B6rster?= Date: Sun, 25 Aug 2024 23:41:06 +0200 Subject: [PATCH] fix some potential crashes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Toni Förster --- Model/MenuModel.swift | 12 +++++++++--- Shared/YatteeApp.swift | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Model/MenuModel.swift b/Model/MenuModel.swift index 42c5daf3..2b50bb45 100644 --- a/Model/MenuModel.swift +++ b/Model/MenuModel.swift @@ -3,7 +3,7 @@ import Foundation final class MenuModel: ObservableObject { static let shared = MenuModel() - private var cancellables = [AnyCancellable]() + private var cancellables = Set() init() { registerChildModel(AccountsModel.shared) @@ -12,10 +12,16 @@ final class MenuModel: ObservableObject { } func registerChildModel(_ model: T?) { - guard !model.isNil else { + guard let model else { return } - cancellables.append(model!.objectWillChange.sink { [weak self] _ in self?.objectWillChange.send() }) + model.objectWillChange + .receive(on: DispatchQueue.main) // Ensure the update occurs on the main thread + .debounce(for: .milliseconds(10), scheduler: DispatchQueue.main) // Debounce to avoid immediate feedback loops + .sink { [weak self] _ in + self?.objectWillChange.send() + } + .store(in: &cancellables) } } diff --git a/Shared/YatteeApp.swift b/Shared/YatteeApp.swift index 699d6204..09a9a746 100644 --- a/Shared/YatteeApp.swift +++ b/Shared/YatteeApp.swift @@ -191,7 +191,7 @@ struct YatteeApp: App { NavigationModel.shared.tabSelection = section ?? .search - DispatchQueue.global(qos: .userInitiated).async { + DispatchQueue.main.async { playlists.load() }