Sunday, June 16, 2024
HomeiOS Developmentios - "Consumer tried a write that's not allowed" MongoDB Atlas Gadget...

ios – “Consumer tried a write that’s not allowed” MongoDB Atlas Gadget Realm Flex Sync Database


I’m creating an iOS app with Realm and MongoDB Atlas. I by no means used Atlas earlier than so this can be a very attention-grabbing studying course of. Atlas has an enormous studying curve, and i’m grateful i determine every part through Youtube, MongoDB Docs, and ChatGPT.

Nevertheless, I’m encourntering an issue when attempting to jot down a object to realm with database guidelines on. I set these guidelines:

{
  "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 mentioned, I’m receiving this error when attempting to jot down a brand new object to the database.

Data: Connection[1] Session[1]: Acquired: ERROR "Consumer tried a write that's not allowed; it has been reverted" (error_code=231, is_fatal=false, error_action=Warning)

I obtained an error just like the earlier than, nevertheless, I mounted it with updating my subscription question. I attempted mainly every part I may consider and the one factor that works is eradicating the database guidelines.

Right here is my mannequin and RealmManager code

class Venue: Object {
    @Persevered(primaryKey: true) var _id: String
    @Persevered var accountId: String
    @Persevered var title: String = ""
    @Persevered var location: String = ""
}

@MainActor
func initialize() async throws {
    person = strive await app.login(credentials: Credentials.nameless)

    let configuration = person?.flexibleSyncConfiguration(initialSubscriptions: { subs in
        if subs.first(named: "userAccounts") == nil {
            subs.append(QuerySubscription<UserAccount>(title: "userAccounts"))
        }
        if subs.first(named: "venues") == nil {
            subs.append(QuerySubscription<Venue>(title: "venues"))
        }
    }, rerunOnOpen: true)

    realm = strive await Realm(configuration: configuration!, downloadBeforeOpen: .all the time)
}

@MainActor
func createVenue(title: String, location: String) async throws {
    guard let person = self.person, let account = strive await getUserAccountById(), let realm = realm else {
        throw NSError(area: "RealmManager", code: -1, userInfo: [NSLocalizedDescriptionKey: "User not logged in"])
    }
    
    let venue = Venue()
    venue._id = UUID().uuidString
    venue.title = title
    venue.accountId = account._id
    venue.location = location
    
    if account._id != "" {
        strive realm.write {
            realm.add(venue)
            
            // Hyperlink the venue to the person
            if let userObject = realm.object(ofType: UserAccount.self, forPrimaryKey: account._id) {
                userObject.venues.append(venue._id)
                realm.add(userObject, replace: .modified)
            }
        }
    }
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments