Skip to content

Add metadatabase URL override for CloudKit sync#426

Open
byMohamedali wants to merge 1 commit intopointfreeco:mainfrom
byMohamedali:codex/metadatabase-url-override
Open

Add metadatabase URL override for CloudKit sync#426
byMohamedali wants to merge 1 commit intopointfreeco:mainfrom
byMohamedali:codex/metadatabase-url-override

Conversation

@byMohamedali
Copy link

Summary

Adds an optional metadatabaseURL override to SQLiteData's CloudKit APIs so callers can choose where the CloudKit metadata database lives.

Changes

  • Add metadatabaseURL: URL? = nil to:
    • SyncEngine.init(for:tables:privateTables:containerIdentifier:metadatabaseURL:defaultZone:startImmediately:delegate:logger:)
    • Database.attachMetadatabase(containerIdentifier:metadatabaseURL:)
  • Preserve existing behavior when no override is provided.
  • Create the parent directory of the resolved metadata URL instead of always creating Application Support.

Motivation

Some platforms, notably tvOS, may not allow the current hardcoded Application Support behavior for this metadata database. The main user database may already be configured to live somewhere else, but CloudKit setup still fails because the metadata store is not configurable.

This change keeps the default behavior intact for existing apps while allowing callers to explicitly provide a writable location when needed.

Example

let metadataURL = FileManager.default
  .urls(for: .cachesDirectory, in: .userDomainMask)
  .first?
  .appendingPathComponent(".SQLiteData.metadata.sqlite")

var configuration = Configuration()
configuration.prepareDatabase { db in
  try db.attachMetadatabase(metadatabaseURL: metadataURL)
}

let database = try SQLiteData.defaultDatabase(
  path: databaseURL.path,
  configuration: configuration
)

let syncEngine = try SyncEngine(
  for: database,
  privateTables: MyTable.self,
  metadatabaseURL: metadataURL
)

@mbrandonw
Copy link
Member

Hi @byMohamedali, it's intended that the metadatabase is created right alongside wherever the user's database is. You can see that's done right here:

return
databaseURL.deletingLastPathComponent().appending(
component: ".\(databaseURL.deletingPathExtension().lastPathComponent)"
)
.appendingPathExtension("metadata\(containerIdentifier.map { "-\($0)" } ?? "").sqlite")

So I'm not sure why this would be needed. Are you positive that the metadatabase is always created in application support?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants