diogenes
diogenes

Reputation: 2109

REALM reason: 'Attempting to modify object outside of a write transaction - call beginWriteTransaction on an RLMRealm instance first.'

I am getting an error when trying to modify a realm object.

It is a simple class and there is actually only one record.

class User: Object{

    @objc dynamic var id = UUID().uuidString
    @objc dynamic var name:String  = ""
    @objc dynamic var email:String = ""
    .....

    static func getInfo() -> User? {
        do {
            let realm = try Realm()
            return realm.objects(User.self).first
        } catch {
            return nil
        }
    }

}

I call the data:

var user = User.getInfo()

And now when I try to modify it I get the following error.

user.name = "test"

*** Terminating app due to uncaught exception 'RLMException', reason: 'Attempting to modify object outside of a write transaction - call beginWriteTransaction on an RLMRealm instance first.'

what am I doing wrong? Thanks.

Thanks.

Upvotes: 7

Views: 13649

Answers (1)

Don
Don

Reputation: 529

user is a Realm instance. Any modifications to user need to be within a realm.write block.

try! realm.write {
    user.name = "test"
}

Upvotes: 12

Related Questions