diff --git a/Documentation/Combine.md b/Documentation/Combine.md index a9004214a..d4e114cc8 100644 --- a/Documentation/Combine.md +++ b/Documentation/Combine.md @@ -398,7 +398,7 @@ let cancellable = hallOfFamePublisher.sink( [`writePublisher(receiveOn:updates:thenRead:)`]: #databasewriterwritepublisherreceiveonupdatesthenread [`migratePublisher(_:receiveOn:)`]: https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasemigrator/migratepublisher(_:receiveon:) [configured]: ../README.md#databasepool-configuration -[database pool]: ../README.md#database-pools -[database queue]: ../README.md#database-queues +[database pool]: https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasepool +[database queue]: https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasequeue [database snapshot]: https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasesnapshot [scheduler]: https://developer.apple.com/documentation/combine/scheduler diff --git a/Documentation/CustomSQLiteBuilds.md b/Documentation/CustomSQLiteBuilds.md index 5df56ab8f..7b1dc090b 100644 --- a/Documentation/CustomSQLiteBuilds.md +++ b/Documentation/CustomSQLiteBuilds.md @@ -21,7 +21,7 @@ GRDB builds SQLite with [swiftlyfalling/SQLiteLib](https://github.com/swiftlyfal 2. Choose your [extra compilation options](https://www.sqlite.org/compile.html). For example, `SQLITE_ENABLE_FTS5`, `SQLITE_ENABLE_PREUPDATE_HOOK`. - It is recommended that you enable the `SQLITE_ENABLE_SNAPSHOT` option. It allows GRDB to optimize [ValueObservation](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/valueobservation) when you use a [Database Pool](../README.md#database-pools). + It is recommended that you enable the `SQLITE_ENABLE_SNAPSHOT` option. It allows GRDB to optimize [ValueObservation](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/valueobservation) when you use a [Database Pool](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasepool). 3. Create a folder named `GRDBCustomSQLite` somewhere in your project directory. diff --git a/Documentation/WhyAdoptGRDB.md b/Documentation/WhyAdoptGRDB.md index 24cd883b3..86fd94b1c 100644 --- a/Documentation/WhyAdoptGRDB.md +++ b/Documentation/WhyAdoptGRDB.md @@ -154,11 +154,11 @@ See [Database Observation](https://swiftpackageindex.com/groue/grdb.swift/docume ### Non-blocking database reads -GRDB ships with two ways to access databases, [database queues](../README.md#database-queues) and [pools](../README.md#database-pools). +GRDB ships with two ways to access databases, [database queues](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasequeue) and [pools](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasepool). Database queues look a lot like FMDB's [FMDatabaseQueue](https://github.com/ccgus/fmdb#using-fmdatabasequeue-and-thread-safety): they serialize database accesses in a serial dispatch queue. There is never more than one thread that is accessing the database. This means that a long running database transaction that happens in a background thread, such as the synchronization of your local database from a remote server, can block your UI. As long as the queue is busy, the main thread can't fetch the values it wants to display on screen. -[Database pools](../README.md#database-pools) can lift these unwanted locks. With database pools, reads are generally non-blocking, unless the maximum number of concurrent reads has been reached (and this maximum number can be configured). +[Database pools](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasepool) can lift these unwanted locks. With database pools, reads are generally non-blocking, unless the maximum number of concurrent reads has been reached (and this maximum number can be configured). ### Strong and clear multi-threading guarantees @@ -287,7 +287,7 @@ Happy GRDB! :gift: [Concurrency Guide]: https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/concurrency [Core Data]: https://developer.apple.com/documentation/coredata -[DatabasePool]: ../README.md#database-pools +[DatabasePool]: https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasepool [Diesel]: http://diesel.rs [FCModel]: https://github.com/marcoarment/FCModel [ValueObservation]: https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/valueobservation diff --git a/GRDB/Documentation.docc/DatabaseConnections.md b/GRDB/Documentation.docc/DatabaseConnections.md index 074eb7aaa..d530d8c56 100644 --- a/GRDB/Documentation.docc/DatabaseConnections.md +++ b/GRDB/Documentation.docc/DatabaseConnections.md @@ -17,17 +17,11 @@ let dbPool = try DatabasePool(path: "/path/to/database.sqlite") The differences are: - `DatabasePool` allows concurrent database accesses (this can improve the performance of multithreaded applications). -- `DatabasePool` opens your SQLite database in the [WAL mode](https://www.sqlite.org/wal.html) (unless ``Configuration/readonly``). +- `DatabasePool` opens your SQLite database in the [WAL mode](https://www.sqlite.org/wal.html). - `DatabaseQueue` supports . **If you are not sure, choose `DatabaseQueue`.** You will always be able to switch to `DatabasePool` later. -Once connected, you can define the database schema, and access the database with raw SQL or high-level Swift apis: - -- -- -- - ## Topics ### Configuring database connections diff --git a/GRDB/Documentation.docc/Extension/DatabasePool.md b/GRDB/Documentation.docc/Extension/DatabasePool.md index d6bce4603..43c64f85e 100644 --- a/GRDB/Documentation.docc/Extension/DatabasePool.md +++ b/GRDB/Documentation.docc/Extension/DatabasePool.md @@ -4,7 +4,7 @@ A database connection that allows concurrent accesses to an SQLite database. ## Usage -**Open a `DatabasePool`** with the path to a database file: +Open a `DatabasePool` with the path to a database file: ```swift import GRDB diff --git a/GRDB/Documentation.docc/Extension/DatabaseQueue.md b/GRDB/Documentation.docc/Extension/DatabaseQueue.md index df9abe58d..856bcc655 100644 --- a/GRDB/Documentation.docc/Extension/DatabaseQueue.md +++ b/GRDB/Documentation.docc/Extension/DatabaseQueue.md @@ -4,7 +4,7 @@ A database connection that serializes accesses to an SQLite database. ## Usage -**Open a `DatabaseQueue`** with the path to a database file: +Open a `DatabaseQueue` with the path to a database file: ```swift import GRDB diff --git a/README.md b/README.md index 1d63d9cdd..6225e11a9 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ Programming tools for both database beginners and SQLite experts: - [Records](#records): Fetching and persistence methods for your custom structs and class hierarchies. - [Query Interface](#the-query-interface): A swift way to avoid the SQL language. - [Associations]: Relations and joins between record types. -- [WAL Mode Support](#database-pools): Extra performance for multi-threaded applications. +- [WAL Mode Support](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasepool): Extra performance for multi-threaded applications. - [Migrations]: Transform your database as your application evolves. - [Database Observation]: Observe database changes and transactions. - [Full-Text Search] @@ -428,7 +428,7 @@ GRDB offers two libraries, `GRDB` and `GRDB-dynamic`. Pick only one. When in dou Database Connections ==================== -GRDB provides two classes for accessing SQLite databases: `DatabaseQueue` and `DatabasePool`: +GRDB provides two classes for accessing SQLite databases: [`DatabaseQueue`] and [`DatabasePool`]: ```swift import GRDB @@ -444,137 +444,7 @@ The differences are: - Database pools open your SQLite database in the [WAL mode](https://www.sqlite.org/wal.html) (unless read-only). - Database queues support [in-memory databases](https://www.sqlite.org/inmemorydb.html). -**If you are not sure, choose DatabaseQueue.** You will always be able to switch to DatabasePool later. - -- [Database Queues](#database-queues) -- [Database Pools](#database-pools) - - -## Database Queues - -**Open a database queue** with the path to a database file: - -```swift -import GRDB - -let dbQueue = try DatabaseQueue(path: "/path/to/database.sqlite") -let inMemoryDBQueue = try DatabaseQueue() -``` - -SQLite creates the database file if it does not already exist. The connection is closed when the database queue gets deinitialized. - -**A database queue can be used from any thread.** The `write` and `read` methods are synchronous, and block the current thread until your database statements are executed in a protected dispatch queue: - -```swift -// Modify the database: -try dbQueue.write { db in - try db.create(table: "place") { ... } - try Place(...).insert(db) -} - -// Read values: -try dbQueue.read { db in - let places = try Place.fetchAll(db) - let placeCount = try Place.fetchCount(db) -} -``` - -Database access methods can return values: - -```swift -let placeCount = try dbQueue.read { db in - try Place.fetchCount(db) -} - -let newPlaceCount = try dbQueue.write { db -> Int in - try Place(...).insert(db) - return try Place.fetchCount(db) -} -``` - -See the [Concurrency] guide for asynchronous database accesses. - -**A database queue serializes accesses to the database**, which means that there is never more than one thread that uses the database. - -- When you don't need to modify the database, prefer the `read` method. It prevents any modification to the database. - -- The `write` method wraps your database statements in a transaction that commits if and only if no error occurs. On the first unhandled error, all changes are reverted, the whole transaction is rollbacked, and the error is rethrown. - - When precise transaction handling is required, see [Transactions and Savepoints]. - -**A database queue needs your application to follow rules in order to deliver its safety guarantees.** Please refer to the [Concurrency] guide. - -See [Database Configuration] for DatabaseQueue options. - -> :bulb: **Tip**: see the [Demo Applications] for sample code. - - -## Database Pools - -**A database pool allows concurrent database accesses.** - -```swift -import GRDB -let dbPool = try DatabasePool(path: "/path/to/database.sqlite") -``` - -SQLite creates the database file if it does not already exist. The connection is closed when the database pool gets deinitialized. - -> **Note**: unless read-only, a database pool opens your database in the SQLite "WAL mode". The WAL mode does not fit all situations. Please have a look at https://www.sqlite.org/wal.html. - -**A database pool can be used from any thread.** The `write` and `read` methods are synchronous, and block the current thread until your database statements are executed in a protected dispatch queue: - -```swift -// Modify the database: -try dbPool.write { db in - try db.create(table: "place") { ... } - try Place(...).insert(db) -} - -// Read values: -try dbPool.read { db in - let places = try Place.fetchAll(db) - let placeCount = try Place.fetchCount(db) -} -``` - -Database access methods can return values: - -```swift -let placeCount = try dbPool.read { db in - try Place.fetchCount(db) -} - -let newPlaceCount = try dbPool.write { db -> Int in - try Place(...).insert(db) - return try Place.fetchCount(db) -} -``` - -See the [Concurrency] guide for asynchronous database accesses. - -**Database pools allow several threads to access the database at the same time:** - -- When you don't need to modify the database, prefer the `read` method, because several threads can perform reads in parallel. - - Reads are generally non-blocking, unless the maximum number of concurrent reads has been reached. In this case, a read has to wait for another read to complete. That maximum number can be [configured](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/configuration). - -- Reads are guaranteed an immutable view of the last committed state of the database, regardless of concurrent writes. This kind of isolation is called [snapshot isolation](https://sqlite.org/isolation.html). - -- Unlike reads, writes are serialized. There is never more than a single thread that is writing into the database. - -- The `write` method wraps your database statements in a transaction that commits if and only if no error occurs. On the first unhandled error, all changes are reverted, the whole transaction is rollbacked, and the error is rethrown. - - When precise transaction handling is required, see [Transactions and Savepoints]. - -- Database pools can take [snapshots](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasesnapshot) of the database. - -**A database pool needs your application to follow rules in order to deliver its safety guarantees.** See the [Concurrency] guide for more details about database pools, how they differ from database queues, and advanced use cases. - -See [Database Configuration] for DatabasePool options. - -> :bulb: **Tip**: see the [Demo Applications] for sample code. - +**If you are not sure, choose `DatabaseQueue`.** You will always be able to switch to `DatabasePool` later. SQLite API @@ -1729,7 +1599,7 @@ When the same query will be used several times in the lifetime of your applicati **Don't cache statements yourself.** -> **Note**: This is because you don't have the necessary tools. Statements are tied to specific SQLite connections and dispatch queues which you don't manage yourself, especially when you use [database pools](#database-pools). A change in the database schema [may, or may not](https://www.sqlite.org/compile.html#max_schema_retry) invalidate a statement. +> **Note**: This is because you don't have the necessary tools. Statements are tied to specific SQLite connections and dispatch queues which you don't manage yourself, especially when you use [database pools]. A change in the database schema [may, or may not](https://www.sqlite.org/compile.html#max_schema_retry) invalidate a statement. Instead, use the `cachedStatement` method. GRDB does all the hard caching and [memory management](#memory-management) stuff for you: @@ -5709,7 +5579,7 @@ See [SQLCipher 4.0.0 Release](https://www.zetetic.net/blog/2018/11/30/sqlcipher- **You can change the passphrase** of an already encrypted database. -When you use a [database queue](#database-queues), open the database with the old passphrase, and then apply the new passphrase: +When you use a [database queue](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasequeue), open the database with the old passphrase, and then apply the new passphrase: ```swift try dbQueue.write { db in @@ -5717,7 +5587,7 @@ try dbQueue.write { db in } ``` -When you use a [database pool](#database-pools), make sure that no concurrent read can happen by changing the passphrase within the `barrierWriteWithoutTransaction` block. You must also ensure all future reads open a new database connection by calling the `invalidateReadOnlyConnections` method: +When you use a [database pool](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasepool), make sure that no concurrent read can happen by changing the passphrase within the `barrierWriteWithoutTransaction` block. You must also ensure all future reads open a new database connection by calling the `invalidateReadOnlyConnections` method: ```swift try dbPool.barrierWriteWithoutTransaction { db in @@ -5865,7 +5735,7 @@ config.prepareDatabase { db in let dbQueue = try DatabaseQueue(path: dbPath, configuration: config) ``` -For the same reason, [database pools](#database-pools), which open SQLite connections on demand, may fail at any time as soon as the passphrase becomes unavailable: +For the same reason, [database pools], which open SQLite connections on demand, may fail at any time as soon as the passphrase becomes unavailable: ```swift // Success if and only if the passphrase is available @@ -5899,7 +5769,7 @@ try source.backup(to: destination) The `backup` method blocks the current thread until the destination database contains the same contents as the source database. -When the source is a [database pool](#database-pools), concurrent writes can happen during the backup. Those writes may, or may not, be reflected in the backup, but they won't trigger any error. +When the source is a [database pool](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasepool), concurrent writes can happen during the backup. Those writes may, or may not, be reflected in the backup, but they won't trigger any error. `Database` has an analogous `backup` method. @@ -6412,7 +6282,7 @@ dbPool.releaseMemoryEventually() **The iOS operating system likes applications that do not consume much memory.** -[Database queues](#database-queues) and [pools](#database-pools) automatically free non-essential memory when the application receives a memory warning, and when the application enters background. +[Database queues] and [pools](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasepool) automatically free non-essential memory when the application receives a memory warning, and when the application enters background. You can opt out of this automatic memory management: @@ -6429,7 +6299,7 @@ let dbQueue = try DatabaseQueue(path: dbPath, configuration: config) // or Datab Data protection can be enabled [globally](https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/AddingCapabilities/AddingCapabilities.html#//apple_ref/doc/uid/TP40012582-CH26-SW30) for all files created by an application. -You can also explicitly protect a database, by configuring its enclosing *directory*. This will not only protect the database file, but also all [temporary files](https://www.sqlite.org/tempfiles.html) created by SQLite (including the persistent `.shm` and `.wal` files created by [database pools](#database-pools)). +You can also explicitly protect a database, by configuring its enclosing *directory*. This will not only protect the database file, but also all [temporary files](https://www.sqlite.org/tempfiles.html) created by SQLite (including the persistent `.shm` and `.wal` files created by [database pools]). For example, to explicitly use [complete](https://developer.apple.com/reference/foundation/fileprotectiontype/1616200-complete) protection: @@ -7046,6 +6916,14 @@ This chapter has [moved](https://swiftpackageindex.com/groue/grdb.swift/document This chapter has [moved](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/configuration). +#### Database Queues + +This chapter has [moved](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasequeue). + +#### Database Pools + +This chapter has [moved](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasepool). + #### Database Snapshots This chapter has [moved](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/concurrency). @@ -7173,4 +7051,8 @@ This chapter has been superseded by [ValueObservation] and [DatabaseRegionObserv [Persistence Methods]: #persistence-methods [persistence methods]: #persistence-methods [RecordError]: #recorderror -[Transactions and Savepoints]: https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/transactions \ No newline at end of file +[Transactions and Savepoints]: https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/transactions +[`DatabaseQueue`]: https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasequeue +[Database queues]: https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasequeue +[`DatabasePool`]: https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasepool +[database pools]: https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/databasepool \ No newline at end of file