I am developing an iOS app with Realm and MongoDB Atlas. I never used Atlas before so this is a very interesting learning process. Atlas has a huge learning curve, and i am thankful i figure out everything via Youtube, MongoDB Docs, and ChatGPT.
However, I am encourntering a problem when trying to write a object to realm with database rules on. I set these rules:
{"roles": [ {"name": "readAndWriteAll","apply_when": {"accountId": "%%user.id" },"document_filters": {"write": true,"read": true },"read": true,"write": true,"insert": true,"delete": true,"search": true } ]}
With that being said, I am receiving this error when trying to write a new object to the database.
Info: Connection[1] Session[1]: Received: ERROR "Client attempted a write that is not allowed; it has been reverted" (error_code=231, is_fatal=false, error_action=Warning)
I received an error like the before, however, I fixed it with updating my subscription query. I tried basically everything I could think of and the only thing that works is removing the database rules.
Here is my model and RealmManager code
class Venue: Object { @Persisted(primaryKey: true) var _id: String @Persisted var accountId: String @Persisted var name: String = "" @Persisted var location: String = ""}@MainActorfunc initialize() async throws { user = try await app.login(credentials: Credentials.anonymous) let configuration = user?.flexibleSyncConfiguration(initialSubscriptions: { subs in if subs.first(named: "userAccounts") == nil { subs.append(QuerySubscription<UserAccount>(name: "userAccounts")) } if subs.first(named: "venues") == nil { subs.append(QuerySubscription<Venue>(name: "venues")) } }, rerunOnOpen: true) realm = try await Realm(configuration: configuration!, downloadBeforeOpen: .always)}@MainActorfunc createVenue(name: String, location: String) async throws { guard let user = self.user, let account = try await getUserAccountById(), let realm = realm else { throw NSError(domain: "RealmManager", code: -1, userInfo: [NSLocalizedDescriptionKey: "User not logged in"]) } let venue = Venue() venue._id = UUID().uuidString venue.name = name venue.accountId = account._id venue.location = location if account._id != "" { try realm.write { realm.add(venue) // Link the venue to the user if let userObject = realm.object(ofType: UserAccount.self, forPrimaryKey: account._id) { userObject.venues.append(venue._id) realm.add(userObject, update: .modified) } } }}