Okan
Okan

Reputation: 1389

Updating an object on realm

I want to update an object with realm. But my app is crashing.

My code:

var findConversations = ShufflePerson.objectsWhere("receiver='\(from)'")
var shuf = findConversations[0] as! ShufflePerson
shuf.unreadMessage=shuf.unreadMessage++
self.realm.beginWriteTransaction()
self.realm.commitWriteTransaction()

Crash Log:

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

How can I fix ?

Upvotes: 0

Views: 1659

Answers (1)

segiddins
segiddins

Reputation: 4120

Try the following:

var findConversations = ShufflePerson.objectsWhere("receiver='\(from)'")
var shuf = findConversations[0] as! ShufflePerson
self.realm.beginWriteTransaction()
shuf.unreadMessage++
self.realm.commitWriteTransaction()

The issue, as the exception message said, was that you were modifying the object outside of the write transaction. Doing that mutation inside the transaction should do the trick!

Upvotes: 2

Related Questions