From fc7dc03cee03754d4e4b083be9c9dfef4823f7b9 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Sat, 18 Aug 2018 23:54:13 +0200 Subject: [PATCH 1/2] don't block main thread during search // FREEBIE --- .../ConversationSearchViewController.swift | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift b/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift index 2f7b16119..5628548cb 100644 --- a/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift +++ b/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift @@ -376,12 +376,19 @@ class ConversationSearchViewController: UITableViewController { return } - self.uiDatabaseConnection.read { transaction in - self.searchResultSet = self.searcher.results(searchText: searchText, transaction: transaction, contactsManager: self.contactsManager) - } - - // TODO: more performant way to do this? - self.tableView.reloadData() + var searchResults: SearchResultSet? + self.uiDatabaseConnection.asyncRead({ transaction in + searchResults = self.searcher.results(searchText: searchText, transaction: transaction, contactsManager: self.contactsManager) + }, + completionBlock: { + guard let results = searchResults else { + owsFail("\(self.logTag) in \(#function) searchResults was unexpectedly nil") + return + } + + self.searchResultSet = results + self.tableView.reloadData() + }) } // MARK: - UIScrollViewDelegate From 781c5353273f20f56f72e237674b955dbc4d625b Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 22 Aug 2018 11:17:56 -0600 Subject: [PATCH 2/2] weak capture self --- .../ConversationSearchViewController.swift | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift b/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift index 5628548cb..759f1fd50 100644 --- a/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift +++ b/Signal/src/ViewControllers/HomeView/ConversationSearchViewController.swift @@ -377,17 +377,21 @@ class ConversationSearchViewController: UITableViewController { } var searchResults: SearchResultSet? - self.uiDatabaseConnection.asyncRead({ transaction in - searchResults = self.searcher.results(searchText: searchText, transaction: transaction, contactsManager: self.contactsManager) + self.uiDatabaseConnection.asyncRead({[weak self] transaction in + guard let strongSelf = self else { return } + searchResults = strongSelf.searcher.results(searchText: searchText, transaction: transaction, contactsManager: strongSelf.contactsManager) }, - completionBlock: { + completionBlock: { [weak self] in + SwiftAssertIsOnMainThread(#function) + guard let strongSelf = self else { return } + guard let results = searchResults else { - owsFail("\(self.logTag) in \(#function) searchResults was unexpectedly nil") + owsFail("\(strongSelf.logTag) in \(#function) searchResults was unexpectedly nil") return } - self.searchResultSet = results - self.tableView.reloadData() + strongSelf.searchResultSet = results + strongSelf.tableView.reloadData() }) }