Implement marking statuses in memory and in database.

This commit is contained in:
Brent Simmons 2017-08-29 13:32:36 -07:00
parent baabf842e1
commit f3bfa8811d
4 changed files with 43 additions and 55 deletions

View File

@ -108,7 +108,29 @@ final class ArticlesTable: DatabaseTable {
func mark(_ articles: Set<Article>, _ statusKey: String, _ flag: Bool) {
// TODO
// Sets flag in both memory and in database.
let articleIDs = articles.flatMap { (article) -> String? in
guard let status = article.status else {
assertionFailure("Each article must have a status.")
return nil
}
if status.boolStatus(forKey: statusKey) == flag {
return nil
}
status.setBoolStatus(flag, forKey: statusKey)
return article.articleID
}
if articleIDs.isEmpty {
return
}
queue.update { (database) in
self.statusesTable.markArticleIDs(Set(articleIDs), statusKey, flag, database)
}
}

View File

@ -86,4 +86,9 @@ extension Set where Element == Article {
return withNilProperty(\Article.status)
}
func statuses() -> Set<ArticleStatus> {
return Set<ArticleStatus>(self.flatMap { $0.status })
}
}

View File

@ -76,14 +76,16 @@ final class StatusesTable: DatabaseTable {
assert(articles.eachHasAStatus())
}
// func markArticles(_ articles: Set<Article>, statusKey: String, flag: Bool) {
//
// // Main thread.
//
// assertNoMissingStatuses(articles)
// let statuses = Set(articles.flatMap { $0.status })
// markArticleStatuses(statuses, statusKey: statusKey, flag: flag)
// }
// MARK: Marking
func markArticleIDs(_ articleIDs: Set<String>, _ statusKey: String, _ flag: Bool, _ database: FMDatabase) {
updateRowsWithValue(NSNumber(value: flag), valueKey: statusKey, whereKey: DatabaseKey.articleID, matches: Array(articleIDs), database: database)
}
// MARK: Updating
// func attachStatuses(_ articles: Set<Article>, _ database: FMDatabase) {
//
@ -155,16 +157,6 @@ private extension StatusesTable {
}
}
// func assertNoMissingStatuses(_ articles: Set<Article>) {
//
// for oneArticle in articles {
// if oneArticle.status == nil {
// assertionFailure("All articles must have a status at this point.")
// return
// }
// }
// }
// MARK: Fetching
// func fetchAndCacheStatusesForArticles(_ articles: Set<Article>, _ database: FMDatabase) {
@ -203,34 +195,6 @@ private extension StatusesTable {
// let status = ArticleStatus(articleID: articleID, row: row)
// cache[articleID] = status
// return status
// }
// MARK: Updating
// func markArticleStatuses(_ statuses: Set<ArticleStatus>, statusKey: String, flag: Bool) {
//
// // Ignore the statuses where status.[statusKey] == flag. Update the remainder and save in database.
//
// var articleIDsToUpdate = Set<String>()
//
// statuses.forEach { (oneStatus) in
//
// if oneStatus.boolStatus(forKey: statusKey) == flag {
// return
// }
//
// oneStatus.setBoolStatus(flag, forKey: statusKey)
// articleIDsToUpdate.insert(oneStatus.articleID)
// }
//
// if !articleIDsToUpdate.isEmpty {
// updateArticleStatusesInDatabase(articleIDsToUpdate, statusKey: statusKey, flag: flag)
// }
// }
// private func updateArticleStatusesInDatabase(_ articleIDs: Set<String>, statusKey: String, flag: Bool) {
//
// updateRowsWithValue(NSNumber(value: flag), valueKey: statusKey, whereKey: DatabaseKey.articleID, matches: Array(articleIDs))
// }
// MARK: Creating

View File

@ -64,14 +64,11 @@ public extension DatabaseTable {
// MARK: Updating
// public func updateRowsWithValue(_ value: Any, valueKey: String, whereKey: String, matches: [Any]) {
//
// queue.update { (database: FMDatabase!) in
//
// let _ = database.rs_updateRows(withValue: value, valueKey: valueKey, whereKey: whereKey, inValues: matches, tableName: self.name)
// }
// }
public func updateRowsWithValue(_ value: Any, valueKey: String, whereKey: String, matches: [Any], database: FMDatabase) {
let _ = database.rs_updateRows(withValue: value, valueKey: valueKey, whereKey: whereKey, inValues: matches, tableName: self.name)
}
// MARK: Saving
public func insertRows(_ dictionaries: [NSDictionary], insertType: RSDatabaseInsertType, in database: FMDatabase) {