Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Sources/SQLiteData/CloudKit/Internal/Metadatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
open "\(url.path(percentEncoded: false))"
"""
)
try FileManager.default.createDirectory(
at: .applicationSupportDirectory,
withIntermediateDirectories: true
)
if !url.isInMemory {
try FileManager.default.createDirectory(
at: url.deletingLastPathComponent(),
withIntermediateDirectories: true
)
}

@Dependency(\.context) var context
guard !url.isInMemory || context != .live
Expand Down
46 changes: 32 additions & 14 deletions Sources/SQLiteData/CloudKit/SyncEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
/// you do not want to be shareable with other users.
/// - containerIdentifier: The container identifier in CloudKit to synchronize to. If omitted
/// the container will be determined from the entitlements of your app.
/// - metadatabaseURL: The location of the metadata database used to track CloudKit sync
/// state. If omitted, a default location is derived from the main database path.
/// - defaultZone: The zone for all records to be stored in.
/// - startImmediately: Determines if the sync engine starts right away or requires an
/// explicit call to ``start()``. By default this argument is `true`.
Expand All @@ -90,6 +92,7 @@
tables: repeat (each T1).Type,
privateTables: repeat (each T2).Type,
containerIdentifier: String? = nil,
metadatabaseURL: URL? = nil,
defaultZone: CKRecordZone = CKRecordZone(zoneName: "co.pointfree.SQLiteData.defaultZone"),
startImmediately: Bool? = nil,
delegate: (any SyncEngineDelegate)? = nil,
Expand Down Expand Up @@ -146,6 +149,7 @@
)
},
userDatabase: userDatabase,
metadatabaseURL: metadatabaseURL,
logger: logger,
delegate: delegate,
tables: allTables,
Expand Down Expand Up @@ -195,6 +199,7 @@
)
},
userDatabase: userDatabase,
metadatabaseURL: metadatabaseURL,
logger: logger,
delegate: delegate,
tables: allTables,
Expand All @@ -215,6 +220,7 @@
SyncEngine
) -> (private: any SyncEngineProtocol, shared: any SyncEngineProtocol),
userDatabase: UserDatabase,
metadatabaseURL: URL? = nil,
logger: Logger,
delegate: (any SyncEngineDelegate)?,
tables: [any SynchronizableTable],
Expand Down Expand Up @@ -258,12 +264,15 @@
self.defaultSyncEngines = defaultSyncEngines
self.userDatabase = userDatabase
self.logger = logger
self.metadatabase = try defaultMetadatabase(
logger: logger,
url: try URL.metadatabase(
let resolvedMetadatabaseURL =
try metadatabaseURL
?? URL.metadatabase(
databasePath: userDatabase.path,
containerIdentifier: container.containerIdentifier
)
self.metadatabase = try defaultMetadatabase(
logger: logger,
url: resolvedMetadatabaseURL
)
self.tablesByName = Dictionary(
uniqueKeysWithValues: self.tables.map { ($0.base.tableName, $0) }
Expand Down Expand Up @@ -2211,9 +2220,15 @@
///
/// See <doc:PreparingDatabase> for more information on preparing your database.
///
/// - Parameter containerIdentifier: The identifier of the CloudKit container used to
/// synchronize data. Defaults to the value set in the app's entitlements.
public func attachMetadatabase(containerIdentifier: String? = nil) throws {
/// - Parameters:
/// - containerIdentifier: The identifier of the CloudKit container used to
/// synchronize data. Defaults to the value set in the app's entitlements.
/// - metadatabaseURL: The location of the attached metadata database. If omitted, a default
/// location is derived from the main database path.
public func attachMetadatabase(
containerIdentifier: String? = nil,
metadatabaseURL: URL? = nil
) throws {
@Dependency(\.context) var context
let containerIdentifier =
containerIdentifier
Expand All @@ -2234,15 +2249,18 @@
"""
)
}
let url = try URL.metadatabase(
databasePath: databasePath,
containerIdentifier: containerIdentifier
)
let url = try metadatabaseURL
?? URL.metadatabase(
databasePath: databasePath,
containerIdentifier: containerIdentifier
)
let path = url.isInMemory ? url.absoluteString : url.path(percentEncoded: false)
try FileManager.default.createDirectory(
at: .applicationSupportDirectory,
withIntermediateDirectories: true
)
if !url.isInMemory {
try FileManager.default.createDirectory(
at: url.deletingLastPathComponent(),
withIntermediateDirectories: true
)
}
let database: any DatabaseWriter =
url.isInMemory
? try DatabaseQueue(path: path)
Expand Down