"Can add, delete, or create objects in Realm in a write transaction"

I am trying to use Realm in my application. I save the data and everything is fine, but when I want to finally read it and send it to Firebase, I get an error message.

@IBAction func registerButtonTapped(sender: BouncingButton) {
    if !monday.isEmpty() && !tuesday.isEmpty() && !wednesday.isEmpty() && !thursday.isEmpty() && !friday.isEmpty() && !saturday.isEmpty() && !sunday.isEmpty() {
        Constants.ActivityIndicator.startActivity()

        self.days.setObject(["open":monday.open,"close":monday.close], forKey: "Monday")
        self.days.setObject(["open":tuesday.open,"close":tuesday.close], forKey: "Tuesday")
        self.days.setObject(["open":wednesday.open,"close":wednesday.close], forKey: "Wednesday")
        self.days.setObject(["open":thursday.open,"close":thursday.close], forKey: "Thursday")
        self.days.setObject(["open":friday.open,"close":friday.close], forKey: "Friday")
        self.days.setObject(["open":saturday.open,"close":saturday.close], forKey: "Saturday")
        self.days.setObject(["open":sunday.open,"close":sunday.close], forKey: "Sunday")

        let email = Constants.clubRegisterForm.email
        let password = Constants.clubRegisterForm.password
        let credential = FIREmailPasswordAuthProvider.credentialWithEmail(email, password: password)

        Constants.FirebaseAPI.Auth?.createUserWithEmail(email, password: password, completion: { (user, error) in
            if error != nil {
                Constants.ActivityIndicator.stopActivity()
                Constants.Alerts.checkErrors(error!)
            } else {
                let changeRequest = user?.profileChangeRequest()
                changeRequest?.photoURL = NSURL(string: "\(Constants.FirebaseAPI.CLUB_PICS)/\(self.uniqueCode)")
                changeRequest?.commitChangesWithCompletion({ (error) in
                    if error != nil {
                        Constants.ActivityIndicator.stopActivity()
                        Constants.Alerts.checkErrors(error!)
                    } else {
                        Constants.FirebaseAPI.Auth?.signInWithCredential(credential, completion: { (user, error) in
                            if error != nil {
                                Constants.ActivityIndicator.stopActivity()
                                Constants.Alerts.checkErrors(error!)
                            } else {
                                do {
                                    let realm = try Realm()
                                    try realm.write {
                                        let userDataFromRealm = realm.objectForPrimaryKey(ClubRegisterForm.self, key: 1)!
                                        let userData: [String:AnyObject] = ["clubName":userDataFromRealm["name"]!,"email":userDataFromRealm["email"]!,"address":userDataFromRealm["address"]!,"price":userDataFromRealm["price"]!,"phoneNumber":userDataFromRealm["phoneNumber"]!,"numberOfCourts":userDataFromRealm["numberOfCourts"]!,"bankAccount":userDataFromRealm["bankAccount"]!,"accountHolder":userDataFromRealm["accountHolder"]!,"branchNumber":userDataFromRealm["branchNumber"]!]

                                        Constants.FirebaseAPI.REF_USERS_CLUBS.child("\(self.uniqueCode)").setValue(userData, withCompletionBlock: { (error, reference) in
                                            if error != nil {
                                                Constants.ActivityIndicator.stopActivity()
                                                Constants.Alerts.checkErrors(error!)
                                            } else {

                                                Constants.FirebaseAPI.REF_USERS_CLUBS.child("\(self.uniqueCode!)").updateChildValues(["OpenHours":self.days,"services":self.clubServices])
                                                Constants.ActivityIndicator.stopActivity()
                                                realm.delete(Constants.clubRegisterForm)
                                                self.performSegueWithIdentifier(Constants.Segues.registered, sender: nil)

                                            }
                                        })
                                    }
                                } catch {
                                    Constants.Alerts.errorOccured()
                                }
                            }
                        })
                    }
                })
            }
        })
    } else {
        Constants.Alerts.emptyFields()

    }
}

This is the error I get:

Application termination due to an uncaught exception "RLMException", reason: "Can only add, delete or create objects in Realm in a write transaction - first call beginWriteTransaction in an instance of RLMRealm. '

It says that I am doing something wrong inside this function, but I am not adding, creating or deleting anything from Realm, just reading.

Decision

I made a stupid mistake, and I tried to make realm.remove off the record, and I forgot about it

+4

Source: https://habr.com/ru/post/1649463/


All Articles