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

Created on 13 Oct 2016  Â·  10Comments  Â·  Source: realm/realm-cocoa

Hi thanks for great library. I use Swift3.0 and whenever I ran this code snippet :

 internal func storeObject(object: MTImage) {
        let realm = try! Realm()
        do {
            try realm.write({
                realm.add(object, update: false)
                print(" Photo stored.")
            })
        }catch let error {
            print(error)
        }
    }

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

Could you help me find an issue ?
Thanks in advance

T-Help

Most helpful comment

In Appdelegate.swift, add this code below
let uiRealm = try! Realm()

In any viewcontrollers or any classes , work with this uiRealm instance.

func storeObject(object: MTImage) {
        do {
            try uiRealm.write({
                uiRealm.add(object, update: false)
                print(" Photo stored.")
            })
        }catch let error {
            print(error)
        }
    }
}

It wouldn't give any bugs or exceptions
I hope you'd solve it soon.

All 10 comments

You get this assertion failure most likely because you're trying to modify object somewhere in other place in your code without write transaction, as stated in Docs:

All changes to an object (addition, modification and deletion) must be done within a write transaction.

Please check if you modify any Object instances without write transaction.

@wirakoon78 were you able to identify the problem? Do you need any other help with this?

No, I solved the issue myself. Thank you

Awesome!

how'd you solve it?

Hi @kishan89. Would you be able to file another issue with your own code samples? Thanks.

My issue is only remotely related. I have a server using digital ocean, and I am trying to read and write from/to my realm on that server rather than a default.realm file on my local machine.

@axiom88-guru Will you please tell how you solved the error

In Appdelegate.swift, add this code below
let uiRealm = try! Realm()

In any viewcontrollers or any classes , work with this uiRealm instance.

func storeObject(object: MTImage) {
        do {
            try uiRealm.write({
                uiRealm.add(object, update: false)
                print(" Photo stored.")
            })
        }catch let error {
            print(error)
        }
    }
}

It wouldn't give any bugs or exceptions
I hope you'd solve it soon.

Here is a link to sample project :
https://github.com/axiom88-guru/moodTracker/tree/master/MoodTracker

On Mon, Nov 20, 2017 at 4:32 AM, Ipek notifications@github.com wrote:

@axiom88-guru https://github.com/axiom88-guru Will you please tell how
you solved the error

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/realm/realm-cocoa/issues/4215#issuecomment-345682666,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ASSn93ft-qz_HhwuUNZ3zo09SZDzJjhgks5s4XFmgaJpZM4KVkS2
.

Was this page helpful?
0 / 5 - 0 ratings