@@ -11,8 +11,7 @@ import SQLite3
1111import RSDatabaseObjC
1212
1313/// Manage a serial queue and a SQLite database.
14- /// It replaces RSDatabaseQueue, which is deprecated.
15- /// Main-thread only.
14+ ///
1615/// Important note: on iOS, the queue can be suspended
1716/// in order to support background refreshing.
1817public final class DatabaseQueue {
@@ -22,7 +21,6 @@ public final class DatabaseQueue {
2221 /// This will return true only on iOS — on macOS it’s always false.
2322 public var isSuspended : Bool {
2423 #if os(iOS)
25- precondition ( Thread . isMainThread)
2624 return _isSuspended
2725 #else
2826 return false
@@ -41,8 +39,6 @@ public final class DatabaseQueue {
4139
4240 /// When init returns, the database will not be suspended: it will be ready for database calls.
4341 public init ( databasePath: String ) {
44- precondition ( Thread . isMainThread)
45-
4642 self . serialDispatchQueue = DispatchQueue ( label: " DatabaseQueue (Serial) - \( databasePath) " , attributes: . initiallyInactive)
4743 self . targetDispatchQueue = DispatchQueue ( label: " DatabaseQueue (Target) - \( databasePath) " )
4844 self . serialDispatchQueue. setTarget ( queue: self . targetDispatchQueue)
@@ -66,7 +62,6 @@ public final class DatabaseQueue {
6662 /// On Mac, suspend() and resume() are no-ops, since there isn’t a need for them.
6763 public func suspend( ) {
6864 #if os(iOS)
69- precondition ( Thread . isMainThread)
7065 guard !_isSuspended else {
7166 return
7267 }
@@ -89,7 +84,6 @@ public final class DatabaseQueue {
8984 /// This is also for iOS only.
9085 public func resume( ) {
9186 #if os(iOS)
92- precondition ( Thread . isMainThread)
9387 guard _isSuspended else {
9488 return
9589 }
@@ -114,15 +108,13 @@ public final class DatabaseQueue {
114108 /// the DatabaseBlock *and* depending on how many other calls have been
115109 /// scheduled on the queue. Use sparingly — prefer async versions.
116110 public func runInDatabaseSync( _ databaseBlock: DatabaseBlock ) {
117- precondition ( Thread . isMainThread)
118111 serialDispatchQueue. sync {
119112 self . _runInDatabase ( self . database, databaseBlock, false )
120113 }
121114 }
122115
123116 /// Run a DatabaseBlock asynchronously.
124117 public func runInDatabase( _ databaseBlock: @escaping DatabaseBlock ) {
125- precondition ( Thread . isMainThread)
126118 serialDispatchQueue. async {
127119 self . _runInDatabase ( self . database, databaseBlock, false )
128120 }
@@ -133,7 +125,6 @@ public final class DatabaseQueue {
133125 /// Nevertheless, it’s best to avoid this because it will block the main thread —
134126 /// prefer the async `runInTransaction` instead.
135127 public func runInTransactionSync( _ databaseBlock: @escaping DatabaseBlock ) {
136- precondition ( Thread . isMainThread)
137128 serialDispatchQueue. sync {
138129 self . _runInDatabase ( self . database, databaseBlock, true )
139130 }
@@ -142,7 +133,6 @@ public final class DatabaseQueue {
142133 /// Run a DatabaseBlock wrapped in a transaction asynchronously.
143134 /// Transactions help performance significantly when updating the database.
144135 public func runInTransaction( _ databaseBlock: @escaping DatabaseBlock ) {
145- precondition ( Thread . isMainThread)
146136 serialDispatchQueue. async {
147137 self . _runInDatabase ( self . database, databaseBlock, true )
148138 }
@@ -151,7 +141,6 @@ public final class DatabaseQueue {
151141 /// Run all the lines that start with "create".
152142 /// Use this to create tables, indexes, etc.
153143 public func runCreateStatements( _ statements: String ) throws {
154- precondition ( Thread . isMainThread)
155144 var error : DatabaseError ? = nil
156145 runInDatabaseSync { result in
157146 switch result {
@@ -177,7 +166,6 @@ public final class DatabaseQueue {
177166 /// since the last vacuum() call. You almost certainly want to call
178167 /// vacuumIfNeeded instead.
179168 public func vacuum( ) {
180- precondition ( Thread . isMainThread)
181169 runInDatabase { result in
182170 result. database? . executeStatements ( " vacuum; " )
183171 }
@@ -189,7 +177,6 @@ public final class DatabaseQueue {
189177 /// - Returns: true if database will be vacuumed.
190178 @discardableResult
191179 public func vacuumIfNeeded( daysBetweenVacuums: Int ) -> Bool {
192- precondition ( Thread . isMainThread)
193180 let defaultsKey = " DatabaseQueue-LastVacuumDate- \( databasePath) "
194181 let minimumVacuumInterval = TimeInterval ( daysBetweenVacuums * ( 60 * 60 * 24 ) ) // Doesn’t have to be precise
195182 let now = Date ( )
0 commit comments