-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathDefaultSyncEngine.swift
More file actions
65 lines (62 loc) · 1.85 KB
/
Copy pathDefaultSyncEngine.swift
File metadata and controls
65 lines (62 loc) · 1.85 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
#if canImport(CloudKit)
import CloudKit
import GRDB
public import Dependencies
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
extension DependencyValues {
/// The default sync engine used by the application.
///
/// Configure this as early as possible in your app's lifetime, like the app entry point in
/// SwiftUI, using `prepareDependencies`:
///
/// ```swift
/// import SQLiteData
/// import SwiftUI
///
/// ```swift
/// @main
/// struct MyApp: App {
/// init() {
/// prepareDependencies {
/// $0.defaultDatabase = try! appDatabase()
/// $0.defaultSyncEngine = SyncEngine(
/// for: $0.defaultDatabase,
/// tables: Item.self
/// )
/// }
/// }
/// // ...
/// }
/// ```
///
/// > Note: You can only prepare the default sync engine a single time in the lifetime of
/// > your app. Attempting to do so more than once will produce a runtime warning.
///
/// Once configured, access the default sync engine anywhere using `@Dependency`:
///
/// ```swift
/// @Dependency(\.defaultSyncEngine) var syncEngine
///
/// syncEngine.acceptShare(metadata: metadata)
/// ```
///
/// See <doc:PreparingDatabase> for more info.
public var defaultSyncEngine: SyncEngine {
get { self[SyncEngine.self] }
set { self[SyncEngine.self] = newValue }
}
}
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
extension SyncEngine: TestDependencyKey {
public static var previewValue: SyncEngine {
try! SyncEngine(for: DatabaseQueue())
}
public static var testValue: SyncEngine {
try! SyncEngine(
for: DatabasePool(
path: URL.temporaryDirectory.appending(path: "\(UUID().uuidString).sqlite").path()
)
)
}
}
#endif