From 2b491217f3fbb33b3d03a962bc58eaf3c8f66881 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 28 Sep 2019 12:18:08 -0700 Subject: [PATCH] =?UTF-8?q?Create=20statusWithRow(=5F=20row:=20FMResultSet?= =?UTF-8?q?,=20articleID:=20String)=20=E2=80=94=C2=A0it=20allows=20us=20to?= =?UTF-8?q?=20avoid=20pulling=20articleID=20from=20the=20row=20twice=20eve?= =?UTF-8?q?ry=20time=20we=E2=80=99re=20creating=20a=20DatabaseArticle.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Frameworks/ArticlesDatabase/ArticlesTable.swift | 13 ++++++------- Frameworks/ArticlesDatabase/StatusesTable.swift | 8 ++++++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Frameworks/ArticlesDatabase/ArticlesTable.swift b/Frameworks/ArticlesDatabase/ArticlesTable.swift index 3ac4de861..a92354045 100644 --- a/Frameworks/ArticlesDatabase/ArticlesTable.swift +++ b/Frameworks/ArticlesDatabase/ArticlesTable.swift @@ -475,16 +475,15 @@ private extension ArticlesTable { func makeDatabaseArticles(with resultSet: FMResultSet) -> Set { let articles = resultSet.mapToSet { (row) -> DatabaseArticle? in - // The resultSet is a result of a JOIN query with the statuses table, - // so we can get the statuses at the same time and avoid additional database lookups. - - guard let status = statusesTable.statusWithRow(resultSet) else { - assertionFailure("Expected status.") + guard let articleID = row.string(forColumn: DatabaseKey.articleID) else { + assertionFailure("Expected articleID.") return nil } - guard let articleID = row.string(forColumn: DatabaseKey.articleID) else { - assertionFailure("Expected articleID.") + // The resultSet is a result of a JOIN query with the statuses table, + // so we can get the statuses at the same time and avoid additional database lookups. + guard let status = statusesTable.statusWithRow(resultSet, articleID: articleID) else { + assertionFailure("Expected status.") return nil } guard let feedID = row.string(forColumn: DatabaseKey.feedID) else { diff --git a/Frameworks/ArticlesDatabase/StatusesTable.swift b/Frameworks/ArticlesDatabase/StatusesTable.swift index 0bb18377d..fa5a741ab 100644 --- a/Frameworks/ArticlesDatabase/StatusesTable.swift +++ b/Frameworks/ArticlesDatabase/StatusesTable.swift @@ -105,17 +105,21 @@ final class StatusesTable: DatabaseTable { guard let articleID = row.string(forColumn: DatabaseKey.articleID) else { return nil } + return statusWithRow(row, articleID: articleID) + } + + func statusWithRow(_ row: FMResultSet, articleID: String) ->ArticleStatus? { if let cachedStatus = cache[articleID] { return cachedStatus } - + guard let dateArrived = row.date(forColumn: DatabaseKey.dateArrived) else { return nil } let articleStatus = ArticleStatus(articleID: articleID, dateArrived: dateArrived, row: row) cache.addStatusIfNotCached(articleStatus) - + return articleStatus }