Compare commits

...
Author SHA1 Message Date
Gwendal Roué c93ba4fba8 WIP 2025-01-17 17:52:52 +01:00
3 changed files with 42 additions and 4 deletions

No files matched your search

+10 -4
View File
@@ -4,11 +4,17 @@ GRDB helps your app deal with Swift and SQLite concurrency.
## Overview
If your app moves slow database jobs off the main thread, so that the user interface remains responsive, then this guide is for you. In the case of apps that share a database with other processes, such as an iOS app and its extensions, don't miss the dedicated <doc:DatabaseSharing> guide after this one.
If your app moves slow database jobs off the main thread, so that the user interface remains responsive, then this guide is for you.
**In all cases, and first and foremost, follow the <doc:Concurrency#Concurrency-Rules> right from the start.**
It is extended with three articles that focus on particular aspects of concurrency:
The other chapters cover, with more details, the fundamentals of SQLite concurrency, and how GRDB makes it manageable from your Swift code.
- <doc:DatabaseSharing>: for apps that **share a database** with other processes, such as an iOS app and its extensions
- <doc:SwiftConcurrency>: the relationships between GRDB and **Swift 6 Concurrency**
- <doc:MainThread>: everything you have to known about GRDB and the main thread.
**In all cases, and first and foremost, follow the Concurrency-Rules, below, right from the start.**
The following chapters cover the fundamentals of SQLite concurrency, and how GRDB makes it manageable from your Swift code.
## Concurrency Rules
@@ -322,7 +328,7 @@ Types that conform to ``TransactionObserver`` can also use those methods in thei
- <doc:SwiftConcurrency>
- <doc:DatabaseSharing>
- <doc:MainThread>
[demo apps]: https://github.com/groue/GRDB.swift/tree/master/Documentation/DemoApps
[`SQLITE_BUSY`]: https://www.sqlite.org/rescode.html#busy
+30
View File
@@ -0,0 +1,30 @@
# Using or Avoiding the Main Thread
Everything you have to known about GRDB and the main thread.
## Overview
**This guide describes the relationship between GRDB and the main thread.** It complements the <doc:Concurrency> guide, that you should read first.
Accessing the database from the main thread is handy. Graphical applications, for example, do not need to show a loading screen until values are fetched and ready for display. They do not need to spawn tasks or any other asynchronous constructs in response to user actions.
There are only two things one should think about when accessing the database from the main thread:
- How long does it take to start the database access?
- How long does it take to execute the database requests?
The first delay depends on *database contention*
The first delay can be slowed down if the database is already busy
SQLite is very fast, and many database accesses are so fast that the user has no time to notice them. In a graphical application, performing fast database accesses from the main thread is handy, because the application does not need to show a loading screen until values are fetched and ready for display.
Other database accesses should be performed in a background thread, in order to avoid user interface freezes.
In the end, accessing the database on or off the main thread is a choice left to the application.
The database can be accessed from the main thread, or from a background thread, at the application convenience. In this guide we explain what you have to know when you intend to access the database from the main thread, or when you want to make sure a database access does not block the main thread. We will discuss plain database accesses (`read`, `write`), as well a database observation with ``ValueObservation``.
### Avoiding the Main Thread
To avoid performing database accesses from the main thread,
<!--@START_MENU_TOKEN@-->Text<!--@END_MENU_TOKEN@-->
@@ -4,6 +4,8 @@ How to best integrate GRDB and Swift Concurrency
## Overview
**This guide describes the relationship between GRDB and Swift 6 Concurrency.** It complements the <doc:Concurrency> guide, that you should read first.
GRDB’s primary goal is to leverage SQLite’s concurrency features for the benefit of application developers. Swift 6 makes it possible to achieve this goal while ensuring data-race safety.
For example, the ``DatabasePool`` connection allows applications to fetch and display database values on screen, even while a background task is writing the results of a network request to disk.