I’m attempting my first pass at an app using Flexible Sync.
The current error I’m getting is when writing to the Realm, this shows up on the server:
For search purposes: Client attempted a write that is outside of permissions or query filters; it has been reverted (ProtocolErrorCode = 231) cannot write to table before opening a subscription on it
Everything I’m doing thus far has been from examples, but with my own objects.
The Idea Object looks like this:
final class Idea: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: String = UUID().uuidString
@Persisted var ownerID: String = "" // user._id
@Persisted var title: String = ""
@Persisted var created: Date = Date()
@Persisted var body: String = ""
}
And my sync permissions look like this:
{
"rules": {},
"defaultRoles": [
{
"name": "owner-write",
"applyWhen": {},
"read": true,
"write": {
"ownerID": "%%user.id"
}
}
]
}
And I’m creating a flexibleSyncConfiguration
with initialSubscription
like this:
if let user = app.currentUser {
let config = user.flexibleSyncConfiguration(initialSubscriptions: { subs in
if subs.first(named: "user_ideas") != nil {
return
} else {
subs.append(QuerySubscription<Idea>(name: "user_ideas") {
$0.ownerID == user.id
})
}
})
ContentView()
.environment(\.realmConfiguration, config)
}
Here is my simplified code in the ContentView(). Add some text to a state variable, show a button, tap the button to add, add the idea object and reset the state variable like this:
struct ContentView: View {
@ObservedResults(Idea.self) var ideas
@State private var newIdea = ""
var body: some View {
VStack(alignment: .leading) {
TextEditor(text: $newIdea)
if newIdea != "" {
Image(systemName: "plus.circle.fill")
.onTapGesture {
addIdea()
}
}
}
}
private func addIdea() {
let idea = Idea()
idea.title = newIdea
idea.ownerID = app.currentUser!.id
$ideas.append(idea)
newIdea = ""
}
}
I assumed this would be an extremely basic implementation, but the error doesn’t make any sense. There is a subscription (I did that while creating the flexible sync configuration) and that is passed in as the environment.
I looked at @Andrew_Morgan’s article about RChat, and although there are lots of spots where subscriptions are added (realm.subscriptions.write
), there are no other mentions of adding data, so I assumed that we do this as we always have.
Am I missing something here?