Can I selectively sync a database schema to CloudKit? #530
Replies: 3 comments
This comment was marked as spam.
This comment was marked as spam.
|
Hi @francisfeng, no we do not support that, and can't really. If we allowed certain rows to be sync'd and not others, it would make it possible to break associations (where a child record is sync'd but its parent record is not). The best way to handle this would be to put all local data into a separate table. There are even a few tricks you can employ to make it pretty nice. For example, say you have a table like this: @Table struct History {
let id: UUID
var contentType: String
var content: String
}Then you can define a local-only version of @Table struct LocalHistory {
var history: History
}This And then a fun thing you can do is create a temporary view to represent the union of both history tables: @Table struct AllHistory {
var history: History
}
AllHistory.createTemporaryView(
as: History
.select { AllHistory.Columns(history: $0) }
.union(
LocalHistory
.select { AllHistory.Columns(history: $0.history) }
)
)And this is the table you can use in the majority of your application. And you can even define triggers on this view so that you are allowed to INSERT into it as if it was a regular table, and under the hood it will re-route the insert to either AllHistory.createTemporaryTrigger(
insteadOf: .insert { new in
History.insert {
($0.contentType, $0.content /* … */)
} values: {
(new.history.contentType, new.history.content)
}
} when: { new in
new.history.contentType.eq("remote")
}
)
AllHistory.createTemporaryTrigger(
insteadOf: .insert { new in
LocalHistory.insert {
($0.contentType, $0.content /* … */)
} values: {
(new.history.contentType, new.history.content)
}
} when: { new in
new.history.contentType.neq("local")
}
)And you can do the same for updates and deletes. That's the rough idea. It takes some work to get right, but once you do you get to forget about |
|
Cool! Thanks for the detailed explanation. This seems like a nice solution. I will explore it. |
Uh oh!
There was an error while loading. Please reload this page.
I have a clipboard manager app that has a
historiestable to save all clipboard history. However, some entries like files are only relevant to the local machine.I wonder if there’s a way to selectively sync a schema based on some criteria, e.g., a
contentTypecolumn inhistoriestable.All reactions