diff --git a/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/CustomOpenAIService+ConfigurableService.swift b/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/CustomOpenAIService+ConfigurableService.swift index d62c2f0c1..e9b75b9c3 100644 --- a/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/CustomOpenAIService+ConfigurableService.swift +++ b/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/CustomOpenAIService+ConfigurableService.swift @@ -111,27 +111,26 @@ private class CustomOpenAIViewModel: ObservableObject { init(service: CustomOpenAIService) { self.service = service - cancellables.append( - Defaults.publisher(.customOpenAIModel, options: []) - .removeDuplicates() - .sink { _ in - self.modelChanged() - } - ) - cancellables.append( - Defaults.publisher(.customOpenAINameKey, options: []) - .removeDuplicates() - .sink { _ in - self.serviceConfigChanged() - } - ) - cancellables.append( - Defaults.publisher(.customOpenAIAvailableModels) - .removeDuplicates() - .sink { _ in - self.modelsTextChanged() - } - ) + Defaults.publisher(.customOpenAIModel, options: []) + .removeDuplicates() + .sink { _ in + self.modelChanged() + } + .store(in: &cancellables) + Defaults.publisher(.customOpenAINameKey, options: []) + .removeDuplicates() + .throttle(for: 0.1, scheduler: DispatchQueue.main, latest: true) + .sink { _ in + self.serviceConfigChanged() + } + .store(in: &cancellables) + Defaults.publisher(.customOpenAIAvailableModels) + .removeDuplicates() + .throttle(for: 0.1, scheduler: DispatchQueue.main, latest: true) + .sink { _ in + self.modelsTextChanged() + } + .store(in: &cancellables) } // MARK: Internal @@ -150,7 +149,7 @@ private class CustomOpenAIViewModel: ObservableObject { // MARK: Private - private var cancellables: [AnyCancellable] = [] + private var cancellables: Set = [] private func modelChanged() { if !validModels.contains(model) { diff --git a/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift b/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift index 8520c4b51..b9d81a2fe 100644 --- a/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift +++ b/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift @@ -103,20 +103,19 @@ private class OpenAIServiceViewModel: ObservableObject { init(service: OpenAIService) { self.service = service - cancellables.append( - Defaults.publisher(.openAIModel, options: []) - .removeDuplicates() - .sink { _ in - self.modelChanged() - } - ) - cancellables.append( - Defaults.publisher(.openAIAvailableModels) - .removeDuplicates() - .sink { _ in - self.modelsTextChanged() - } - ) + Defaults.publisher(.openAIModel, options: []) + .removeDuplicates() + .sink { _ in + self.modelChanged() + } + .store(in: &cancellables) + Defaults.publisher(.openAIAvailableModels) + .removeDuplicates() + .throttle(for: 0.1, scheduler: DispatchQueue.main, latest: true) + .sink { _ in + self.modelsTextChanged() + } + .store(in: &cancellables) } // MARK: Internal @@ -135,7 +134,7 @@ private class OpenAIServiceViewModel: ObservableObject { // MARK: Private - private var cancellables: [AnyCancellable] = [] + private var cancellables: Set = [] private func modelChanged() { // Currently, user of low os versions can change OpenAI model using URL scheme, like easydict://writeKeyValue?EZOpenAIModelKey=gpt-4