I am trying to share documents with a link website.com/items/:id
.
class Item: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var userId: String
@Persisted var name: String
}
If a user opens the link (I am using universal links so they are opened in the app), I want to start synchronizing based on the document id so that the item can be edited on any device.
class SharedWithMe: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var userId: String
@Persisted var item: Item
}
To accomplish this, I introduce a SharedWithMe
document that has a user id.
extension User {
func createFlexibleConfiguration() -> Realm.Configuration {
let config = self.flexibleSyncConfiguration(initialSubscriptions: { subs in
if subs.first(named: "user_items") == nil {
subs.append(QuerySubscription<Item>(name: "user_items") {
$0.userId == self.id
})
}
if subs.first(named: "shared_with_me") == nil {
subs.append(QuerySubscription<SharedWithMe>(name: "shared_with_me") {
$0.userId == self.id
})
// subs.append(QuerySubscription<Item>(name: "shared_items") {
// $0.id == SharedWithMe.item.id
// })
}
})
return config
}
}
I am not sure how to write a more advanced subscription query for Item
that searches the SharedWithMe
collection.
I could create a subscription when opening a link, but then other devices wouldn’t be aware of that subscription.
let parsedId = "123"
let sharedWithMe = SharedWithMe()
sharedWithMe.userId = user.id
sharedWithMe.item.id = parsedId // Cannot assign to property: 'id' is a get-only property
subs.append(QuerySubscription<Item>(name: parsedId) {
$0.id.stringValue = parsedId
})