This commit is contained in:
Maurice Parker 2019-12-08 18:14:41 -07:00
commit 628be90302
3 changed files with 25 additions and 16 deletions

View File

@ -277,19 +277,11 @@ final class ArticlesTable: DatabaseTable {
self.callUpdateArticlesCompletionBlock(newArticles, updatedArticles, completion) //7 self.callUpdateArticlesCompletionBlock(newArticles, updatedArticles, completion) //7
// 8. Update search index. // 8. Update search index.
var articlesToIndex = Set<Article>()
if let newArticles = newArticles { if let newArticles = newArticles {
articlesToIndex.formUnion(newArticles) self.searchTable.indexNewArticles(newArticles, database)
} }
if let updatedArticles = updatedArticles { if let updatedArticles = updatedArticles {
articlesToIndex.formUnion(updatedArticles) self.searchTable.indexUpdatedArticles(updatedArticles, database)
}
let articleIDsToIndex = articlesToIndex.articleIDs()
if articleIDsToIndex.isEmpty {
return
}
DispatchQueue.main.async {
self.searchTable.ensureIndexedArticles(for: articleIDsToIndex)
} }
} }
} }
@ -440,6 +432,7 @@ final class ArticlesTable: DatabaseTable {
if articleIDs.isEmpty { if articleIDs.isEmpty {
return return
} }
self.searchTable.ensureIndexedArticles(articleIDs, database)
DispatchQueue.main.async { DispatchQueue.main.async {
self.indexUnindexedArticles() self.indexUnindexedArticles()

View File

@ -45,6 +45,10 @@ final class ArticleSearchInfo: Hashable {
self.searchRowID = searchRowID self.searchRowID = searchRowID
} }
convenience init(article: Article) {
self.init(articleID: article.articleID, title: article.title, contentHTML: article.contentHTML, contentText: article.contentText, summary: article.summary, searchRowID: nil)
}
// MARK: Hashable // MARK: Hashable
public func hash(into hasher: inout Hasher) { public func hash(into hasher: inout Hasher) {
@ -77,12 +81,8 @@ final class SearchTable: DatabaseTable {
self.ensureIndexedArticles(articleIDs, database) self.ensureIndexedArticles(articleIDs, database)
} }
} }
}
// MARK: - Private
private extension SearchTable {
/// Add to, or update, the search index for articles with specified IDs.
func ensureIndexedArticles(_ articleIDs: Set<String>, _ database: FMDatabase) { func ensureIndexedArticles(_ articleIDs: Set<String>, _ database: FMDatabase) {
guard let articlesTable = articlesTable else { guard let articlesTable = articlesTable else {
return return
@ -98,6 +98,22 @@ private extension SearchTable {
updateIndexForArticles(indexedArticles, database) updateIndexForArticles(indexedArticles, database)
} }
/// Index new articles.
func indexNewArticles(_ articles: Set<Article>, _ database: FMDatabase) {
let articleSearchInfos = Set(articles.map{ ArticleSearchInfo(article: $0) })
performInitialIndexForArticles(articleSearchInfos, database)
}
/// Index updated articles.
func indexUpdatedArticles(_ articles: Set<Article>, _ database: FMDatabase) {
ensureIndexedArticles(articles.articleIDs(), database)
}
}
// MARK: - Private
private extension SearchTable {
func performInitialIndexForArticles(_ articles: Set<ArticleSearchInfo>, _ database: FMDatabase) { func performInitialIndexForArticles(_ articles: Set<ArticleSearchInfo>, _ database: FMDatabase) {
articles.forEach { performInitialIndex($0, database) } articles.forEach { performInitialIndex($0, database) }
} }

View File

@ -1,7 +1,7 @@
// High Level Settings common to both the iOS application and any extensions we bundle with it // High Level Settings common to both the iOS application and any extensions we bundle with it
MARKETING_VERSION = 5.0 MARKETING_VERSION = 5.0
CURRENT_PROJECT_VERSION = 17 CURRENT_PROJECT_VERSION = 18
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon