diff --git a/Frameworks/Account/Account.xcodeproj/project.pbxproj b/Frameworks/Account/Account.xcodeproj/project.pbxproj index c41172a75..7bd076a2d 100644 --- a/Frameworks/Account/Account.xcodeproj/project.pbxproj +++ b/Frameworks/Account/Account.xcodeproj/project.pbxproj @@ -74,6 +74,7 @@ 844B297F210CE37E004020B3 /* UnreadCountProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 844B297E210CE37E004020B3 /* UnreadCountProvider.swift */; }; 844B2981210CE3BF004020B3 /* RSWeb.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 844B2980210CE3BF004020B3 /* RSWeb.framework */; }; 8469F81C1F6DD15E0084783E /* Account.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848935101F62486800CEBD24 /* Account.swift */; }; + 846CA1882392349E00B55117 /* SyncDatabase.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 846CA1872392349E00B55117 /* SyncDatabase.framework */; }; 846E77451F6EF9B900A165E2 /* Container.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8419740D1F6DD25F006346C4 /* Container.swift */; }; 846E774F1F6EF9C000A165E2 /* LocalAccountDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8419742C1F6DDE84006346C4 /* LocalAccountDelegate.swift */; }; 846E77501F6EF9C400A165E2 /* LocalAccountRefresher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8419742D1F6DDE96006346C4 /* LocalAccountRefresher.swift */; }; @@ -292,6 +293,7 @@ 844B297C2106C7EC004020B3 /* WebFeed.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebFeed.swift; sourceTree = ""; }; 844B297E210CE37E004020B3 /* UnreadCountProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnreadCountProvider.swift; sourceTree = ""; }; 844B2980210CE3BF004020B3 /* RSWeb.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = RSWeb.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 846CA1872392349E00B55117 /* SyncDatabase.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SyncDatabase.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 846E77531F6F00E300A165E2 /* AccountManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountManager.swift; sourceTree = ""; }; 848934F61F62484F00CEBD24 /* Account.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Account.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 848934FA1F62484F00CEBD24 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; diff --git a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift index d80eae09f..948deefa2 100644 --- a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift +++ b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift @@ -22,21 +22,29 @@ public typealias UpdateArticlesCompletionBlock = (Set
?, Set
?) public final class ArticlesDatabase { + /// When ArticlesDatabase is suspended, database calls will crash the app. + public var isSuspended: Bool { + return queue.isSuspended + } + private let articlesTable: ArticlesTable + private let queue: DatabaseQueue public init(databaseFilePath: String, accountID: String) { - let queue = RSDatabaseQueue(filepath: databaseFilePath, excludeFromBackup: false) + let queue = DatabaseQueue(databasePath: databaseFilePath) + self.queue = queue self.articlesTable = ArticlesTable(name: DatabaseTableName.articles, accountID: accountID, queue: queue) - queue.createTables(usingStatements: ArticlesDatabase.tableCreationStatements) - queue.update { (database) in + queue.runCreateStatements(ArticlesDatabase.tableCreationStatements) + queue.runInDatabase { database in if !self.articlesTable.containsColumn("searchRowID", in: database) { database.executeStatements("ALTER TABLE articles add column searchRowID INTEGER;") } database.executeStatements("CREATE INDEX if not EXISTS articles_searchRowID on articles(searchRowID);") database.executeStatements("DROP TABLE if EXISTS tags;DROP INDEX if EXISTS tags_tagName_index;DROP INDEX if EXISTS articles_feedID_index;DROP INDEX if EXISTS statuses_read_index;") } - queue.vacuumIfNeeded() + + queue.vacuumIfNeeded(daysBetweenVacuums: 9) DispatchQueue.main.async { self.articlesTable.indexUnindexedArticles() } @@ -161,6 +169,19 @@ public final class ArticlesDatabase { return articlesTable.mark(articles, statusKey, flag) } + // MARK: - Suspend and Resume (for iOS) + + /// Close the database and stop running database calls. + /// Any pending calls will complete first. + public func suspend() { + queue.suspend() + } + + /// Open the database and allow for running database calls again. + public func resume() { + queue.resume() + } + // MARK: - Caches /// Call to free up some memory. Should be done when the app is backgrounded, for instance. diff --git a/Frameworks/ArticlesDatabase/ArticlesTable.swift b/Frameworks/ArticlesDatabase/ArticlesTable.swift index 153a74368..59e65f4ad 100644 --- a/Frameworks/ArticlesDatabase/ArticlesTable.swift +++ b/Frameworks/ArticlesDatabase/ArticlesTable.swift @@ -16,7 +16,7 @@ final class ArticlesTable: DatabaseTable { let name: String private let accountID: String - private let queue: RSDatabaseQueue + private let queue: DatabaseQueue private let statusesTable: StatusesTable private let authorsLookupTable: DatabaseLookupTable private let attachmentsLookupTable: DatabaseLookupTable @@ -32,7 +32,7 @@ final class ArticlesTable: DatabaseTable { private typealias ArticlesFetchMethod = (FMDatabase) -> Set
- init(name: String, accountID: String, queue: RSDatabaseQueue) { + init(name: String, accountID: String, queue: DatabaseQueue) { self.name = name self.accountID = accountID @@ -168,7 +168,7 @@ final class ArticlesTable: DatabaseTable { func fetchArticlesMatching(_ searchString: String) -> Set
{ var articles: Set
= Set
() - queue.fetchSync { (database) in + queue.runInDatabaseSync { (database) in articles = self.fetchArticlesMatching(searchString, database) } return articles @@ -255,7 +255,7 @@ final class ArticlesTable: DatabaseTable { articleIDs.formUnion(parsedItems.articleIDs()) } - self.queue.update { (database) in + self.queue.runInTransaction { (database) in let statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, read, database) //1 assert(statusesDictionary.count == articleIDs.count) @@ -299,7 +299,7 @@ final class ArticlesTable: DatabaseTable { } func ensureStatuses(_ articleIDs: Set, _ defaultRead: Bool, _ statusKey: ArticleStatus.Key, _ flag: Bool, completionHandler: (() -> ())? = nil) { - self.queue.update { (database) in + queue.runInTransaction { (database) in let statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, defaultRead, database) let statuses = Set(statusesDictionary.values) self.statusesTable.mark(statuses, statusKey, flag, database) @@ -319,7 +319,7 @@ final class ArticlesTable: DatabaseTable { var unreadCountDictionary = UnreadCountDictionary() - queue.fetch { (database) in + queue.runInDatabase { (database) in for webFeedID in webFeedIDs { unreadCountDictionary[webFeedID] = self.fetchUnreadCount(webFeedID, database) } @@ -338,7 +338,7 @@ final class ArticlesTable: DatabaseTable { return } - queue.fetch { (database) in + queue.runInDatabase { (database) in let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(webFeedIDs.count))! let sql = "select count(*) from articles natural join statuses where feedID in \(placeholders) and (datePublished > ? or (datePublished is null and dateArrived > ?)) and read=0 and userDeleted=0;" @@ -360,7 +360,7 @@ final class ArticlesTable: DatabaseTable { let cutoffDate = articleCutoffDate - queue.fetch { (database) in + queue.runInDatabase { (database) in let sql = "select distinct feedID, count(*) from articles natural join statuses where read=0 and userDeleted=0 and (starred=1 or dateArrived>?) group by feedID;" guard let resultSet = database.executeQuery(sql, withArgumentsIn: [cutoffDate]) else { @@ -390,7 +390,7 @@ final class ArticlesTable: DatabaseTable { return } - queue.fetch { (database) in + queue.runInDatabase { (database) in let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(webFeedIDs.count))! let sql = "select count(*) from articles natural join statuses where feedID in \(placeholders) and read=0 and starred=1 and userDeleted=0;" let parameters = Array(webFeedIDs) as [Any] @@ -419,7 +419,7 @@ final class ArticlesTable: DatabaseTable { func mark(_ articles: Set
, _ statusKey: ArticleStatus.Key, _ flag: Bool) -> Set? { var statuses: Set? - self.queue.updateSync { (database) in + self.queue.runInTransactionSync { (database) in statuses = self.statusesTable.mark(articles.statuses(), statusKey, flag, database) } return statuses @@ -428,7 +428,7 @@ final class ArticlesTable: DatabaseTable { // MARK: - Indexing func indexUnindexedArticles() { - queue.fetch { (database) in + queue.runInDatabase { (database) in let sql = "select articleID from articles where searchRowID is null limit 500;" guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else { return @@ -448,7 +448,7 @@ final class ArticlesTable: DatabaseTable { // MARK: - Caches func emptyCaches() { - queue.run { _ in + queue.runInDatabase { _ in self.databaseArticlesCache = [String: DatabaseArticle]() } } @@ -462,7 +462,7 @@ final class ArticlesTable: DatabaseTable { if webFeedIDs.isEmpty { return } - queue.run { (database) in + queue.runInDatabase { (database) in let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(webFeedIDs.count))! let sql = "select articleID from articles where feedID not in \(placeholders);" let parameters = Array(webFeedIDs) as [Any] @@ -487,14 +487,14 @@ private extension ArticlesTable { private func fetchArticles(_ fetchMethod: @escaping ArticlesFetchMethod) -> Set
{ var articles = Set
() - queue.fetchSync { (database) in + queue.runInDatabaseSync { (database) in articles = fetchMethod(database) } return articles } private func fetchArticlesAsync(_ fetchMethod: @escaping ArticlesFetchMethod, _ callback: @escaping ArticleSetBlock) { - queue.fetch { (database) in + queue.runInDatabase { (database) in let articles = fetchMethod(database) DispatchQueue.main.async { callback(articles) diff --git a/Frameworks/ArticlesDatabase/SearchTable.swift b/Frameworks/ArticlesDatabase/SearchTable.swift index d0c95838a..8007b366d 100644 --- a/Frameworks/ArticlesDatabase/SearchTable.swift +++ b/Frameworks/ArticlesDatabase/SearchTable.swift @@ -61,10 +61,10 @@ final class ArticleSearchInfo: Hashable { final class SearchTable: DatabaseTable { let name = "search" - private let queue: RSDatabaseQueue + private let queue: DatabaseQueue private weak var articlesTable: ArticlesTable? - init(queue: RSDatabaseQueue, articlesTable: ArticlesTable) { + init(queue: DatabaseQueue, articlesTable: ArticlesTable) { self.queue = queue self.articlesTable = articlesTable } @@ -73,7 +73,7 @@ final class SearchTable: DatabaseTable { if articleIDs.isEmpty { return } - queue.update { (database) in + queue.runInTransaction { (database) in self.ensureIndexedArticles(articleIDs, database) } } diff --git a/Frameworks/ArticlesDatabase/StatusesTable.swift b/Frameworks/ArticlesDatabase/StatusesTable.swift index 65615c1b0..745bd9efb 100644 --- a/Frameworks/ArticlesDatabase/StatusesTable.swift +++ b/Frameworks/ArticlesDatabase/StatusesTable.swift @@ -19,9 +19,9 @@ final class StatusesTable: DatabaseTable { let name = DatabaseTableName.statuses private let cache = StatusCache() - private let queue: RSDatabaseQueue + private let queue: DatabaseQueue - init(queue: RSDatabaseQueue) { + init(queue: DatabaseQueue) { self.queue = queue } @@ -88,7 +88,7 @@ final class StatusesTable: DatabaseTable { func fetchArticleIDs(_ sql: String) -> Set { var articleIDs = Set() - queue.fetchSync { (database) in + queue.runInDatabaseSync { (database) in guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else { return } diff --git a/Frameworks/SyncDatabase/SyncDatabase.swift b/Frameworks/SyncDatabase/SyncDatabase.swift index 2269afe7b..568da3353 100644 --- a/Frameworks/SyncDatabase/SyncDatabase.swift +++ b/Frameworks/SyncDatabase/SyncDatabase.swift @@ -9,20 +9,27 @@ import Foundation import RSDatabase -public final class SyncDatabase { - - private let syncStatusTable: SyncStatusTable - - public init(databaseFilePath: String) { - - let queue = RSDatabaseQueue(filepath: databaseFilePath, excludeFromBackup: false) - self.syncStatusTable = SyncStatusTable(queue: queue) - - queue.createTables(usingStatementsSync: SyncDatabase.tableCreationStatements) - queue.vacuumIfNeeded() +public struct SyncDatabase { + /// When SyncDatabase is suspended, database calls will crash the app. + public var isSuspended: Bool { + return queue.isSuspended } - + + private let syncStatusTable: SyncStatusTable + private let queue: DatabaseQueue + + public init(databaseFilePath: String) { + let queue = DatabaseQueue(databasePath: databaseFilePath) + queue.runCreateStatements(SyncDatabase.tableCreationStatements) + queue.vacuumIfNeeded(daysBetweenVacuums: 11) + self.queue = queue + + self.syncStatusTable = SyncStatusTable(queue: queue) + } + + // MARK: - API + public func insertStatuses(_ statuses: [SyncStatus], completionHandler: (() -> ())? = nil) { syncStatusTable.insertStatuses(statuses, completionHandler: completionHandler) } @@ -42,7 +49,19 @@ public final class SyncDatabase { public func deleteSelectedForProcessing(_ articleIDs: [String], completionHandler: (() -> ())? = nil) { syncStatusTable.deleteSelectedForProcessing(articleIDs, completionHandler: completionHandler) } - + + // MARK: - Suspend and Resume (for iOS) + + /// Close the database and stop running database calls. + /// Any pending calls will complete first. + public func suspend() { + queue.suspend() + } + + /// Open the database and allow for running database calls again. + public func resume() { + queue.resume() + } } // MARK: - Private diff --git a/Frameworks/SyncDatabase/SyncStatusTable.swift b/Frameworks/SyncDatabase/SyncStatusTable.swift index a038426ff..23b55c2d5 100644 --- a/Frameworks/SyncDatabase/SyncStatusTable.swift +++ b/Frameworks/SyncDatabase/SyncStatusTable.swift @@ -10,21 +10,19 @@ import Foundation import Articles import RSDatabase -final class SyncStatusTable: DatabaseTable { - +struct SyncStatusTable: DatabaseTable { + let name = DatabaseTableName.syncStatus - private let queue: RSDatabaseQueue - - init(queue: RSDatabaseQueue) { + private let queue: DatabaseQueue + + init(queue: DatabaseQueue) { self.queue = queue } func selectForProcessing() -> [SyncStatus] { - var statuses: Set? = nil - self.queue.updateSync { database in - + queue.runInDatabaseSync { database in let updateSQL = "update syncStatus set selected = true" database.executeUpdate(updateSQL, withArgumentsIn: nil) @@ -32,32 +30,26 @@ final class SyncStatusTable: DatabaseTable { if let resultSet = database.executeQuery(selectSQL, withArgumentsIn: nil) { statuses = resultSet.mapToSet(self.statusWithRow) } - } return statuses != nil ? Array(statuses!) : [SyncStatus]() - } func selectPendingCount() -> Int { - var count: Int = 0 - self.queue.fetchSync { (database) in + queue.runInDatabaseSync { database in let sql = "select count(*) from syncStatus" if let resultSet = database.executeQuery(sql, withArgumentsIn: nil) { - resultSet.next() - count = Int(resultSet.int(forColumnIndex: 0)) + count = numberWithCountResultSet(resultSet) } - } return count - } func resetSelectedForProcessing(_ articleIDs: [String], completionHandler: (() -> ())? = nil) { - self.queue.update { database in + queue.runInTransaction { database in let parameters = articleIDs.map { $0 as AnyObject } let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(articleIDs.count))! let updateSQL = "update syncStatus set selected = false where articleID in \(placeholders)" @@ -69,7 +61,7 @@ final class SyncStatusTable: DatabaseTable { } func deleteSelectedForProcessing(_ articleIDs: [String], completionHandler: (() -> ())? = nil) { - self.queue.update { database in + queue.runInTransaction { database in let parameters = articleIDs.map { $0 as AnyObject } let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(articleIDs.count))! let deleteSQL = "delete from syncStatus where articleID in \(placeholders)" @@ -81,7 +73,7 @@ final class SyncStatusTable: DatabaseTable { } func insertStatuses(_ statuses: [SyncStatus], completionHandler: (() -> ())? = nil) { - self.queue.update { database in + queue.runInTransaction { database in let statusArray = statuses.map { $0.databaseDictionary() } self.insertRows(statusArray, insertType: .orReplace, in: database) if let handler = completionHandler { @@ -89,13 +81,11 @@ final class SyncStatusTable: DatabaseTable { } } } - } private extension SyncStatusTable { func statusWithRow(_ row: FMResultSet) -> SyncStatus? { - guard let articleID = row.string(forColumn: DatabaseKey.articleID), let rawKey = row.string(forColumn: DatabaseKey.key), let key = ArticleStatus.Key(rawValue: rawKey) else { @@ -106,6 +96,5 @@ private extension SyncStatusTable { let selected = row.bool(forColumn: DatabaseKey.selected) return SyncStatus(articleID: articleID, key: key, flag: flag, selected: selected) - } }