forked from pointfreeco/sqlite-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionDemo.swift
More file actions
109 lines (100 loc) · 2.81 KB
/
TransactionDemo.swift
File metadata and controls
109 lines (100 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import SQLiteData
import SwiftUI
struct TransactionDemo: SwiftUICaseStudy {
let readMe = """
This demonstrates how to use the `@Fetch` tool to perform multiple SQLite queries in a single \
database transaction. If you need to fetch multiple pieces of data from the database that \
all tend to change together, then performing those queries in a single transaction can be \
more performant.
For example, if you need to fetch rows from a table as well as a count of the rows in the \
table, then those two pieces of data will tend to change at the same time (though not always). \
So, it can be better to perform the select and count as two different queries in the same \
database transaction.
"""
let caseStudyTitle = "Database Transactions"
@Fetch(Facts(), animation: .default)
private var facts = Facts.Value()
@Dependency(\.defaultDatabase) var database
var body: some View {
List {
Section {
Text("Facts: \(facts.count)")
.font(.largeTitle)
.bold()
.contentTransition(.numericText(value: Double(facts.count)))
}
Section {
ForEach(facts.facts) { fact in
Text(fact.body)
}
}
}
.task {
do {
var number = 0
while true {
try await Task.sleep(for: .seconds(1))
number += 1
let fact = try await String(
decoding: URLSession.shared
.data(from: URL(string: "http://numberapi.com/\(number)")!).0,
as: UTF8.self
)
try await database.write { db in
try Fact.insert {
Fact.Draft(body: fact)
}
.execute(db)
}
}
} catch {}
}
}
private struct Facts: FetchKeyRequest {
struct Value {
var facts: [Fact] = []
var count = 0
}
func fetch(_ db: Database) throws -> Value {
try Value(
facts: Fact.order { $0.id.desc() }.fetchAll(db),
count: Fact.fetchCount(db)
)
}
}
}
@Table
nonisolated private struct Fact: Identifiable {
static let databaseTableName = "facts"
let id: Int
var body: String
}
extension DatabaseWriter where Self == DatabaseQueue {
static var transactionDemoDatabase: Self {
let databaseQueue = try! DatabaseQueue()
var migrator = DatabaseMigrator()
migrator.registerMigration("Create 'facts' table") { db in
try #sql(
"""
CREATE TABLE "facts" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"body" TEXT NOT NULL
) STRICT
"""
)
.execute(db)
}
try! migrator.migrate(databaseQueue)
return databaseQueue
}
}
#Preview {
let _ = prepareDependencies {
$0.defaultDatabase = .transactionDemoDatabase
}
NavigationStack {
CaseStudyView {
TransactionDemo()
}
}
}