From 519446c5a8bbb592a2f744f9466f1f4a2b8de0ec Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 9 Sep 2023 12:28:53 -0400 Subject: [PATCH] Fix crash if autocomplete controller dealloc'd before search task starts --- .../ComposeUI/Controllers/AutocompleteEmojisController.swift | 4 ++-- .../Controllers/AutocompleteHashtagsController.swift | 4 ++-- .../Controllers/AutocompleteMentionsController.swift | 5 +++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/AutocompleteEmojisController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/AutocompleteEmojisController.swift index 3606a5385a..3083d8c6e9 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/AutocompleteEmojisController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/AutocompleteEmojisController.swift @@ -36,8 +36,8 @@ class AutocompleteEmojisController: ViewController { .removeDuplicates() .sink { [unowned self] query in self.searchTask?.cancel() - self.searchTask = Task { - await self.queryChanged(query) + self.searchTask = Task { [weak self] in + await self?.queryChanged(query) } } } diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/AutocompleteHashtagsController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/AutocompleteHashtagsController.swift index 3d964d1ef8..674fce4d08 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/AutocompleteHashtagsController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/AutocompleteHashtagsController.swift @@ -34,8 +34,8 @@ class AutocompleteHashtagsController: ViewController { .debounce(for: .milliseconds(250), scheduler: DispatchQueue.main) .sink { [unowned self] query in self.searchTask?.cancel() - self.searchTask = Task { - await self.queryChanged(query) + self.searchTask = Task { [weak self] in + await self?.queryChanged(query) } } } diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/AutocompleteMentionsController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/AutocompleteMentionsController.swift index 0acfc1cda2..4272dc5f7e 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/AutocompleteMentionsController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/AutocompleteMentionsController.swift @@ -36,8 +36,9 @@ class AutocompleteMentionsController: ViewController { .debounce(for: .milliseconds(250), scheduler: DispatchQueue.main) .sink { [unowned self] query in self.searchTask?.cancel() - self.searchTask = Task { - await self.queryChanged(query) + // weak in case the autocomplete controller is dealloc'd racing with the task starting + self.searchTask = Task { [weak self] in + await self?.queryChanged(query) } } }