0
votes

Whenever I try to check equality between a property on a Realm object and a NSInteger, it throws the following exception:

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

However I am not modifying the object, just accessing it. Do I have to start a write transaction to check equality?

If I put a breakpoint right at the start of the if statement, the next step in throws the exception.

//message is a RLMObject stored in a RLMResults array
if (message.status == 3 || message.status == 4) {
    NSLog(@"Message status: %ld", (long)message.status);
}
1
There's no reason that the code you shared will trigger the exception you mention. I'd suggest using an exception breakpoint in Xcode to break at the point the exception is thrown, and then looking up the backtrace to your code to confirm which statement is performing the write outside of a transaction. - bdash
@bdash I am still having this issue however the only place it is crashing is in didSelectRowAtIndexPath for my tableview delegate. Calling this code anywhere else in the same view controller works fine. Putting it around a begin and write transaction doesn't help either. - Dhananjay Suresh
Have you set an breakpoint on Objective-C exceptions as I suggested? What did it reveal? - bdash
@bdash Moving some code around I realized the problem. The primary key of the message was getting passed to a view controller few lines above this code. If I don't pass it then there is no exception thrown. Should I be starting a write transaction to pass a property to the next view controller? - Dhananjay Suresh
@bdash Thank you for your help. It was my stupidity, I didn't realize I was actually modifying the message during load of the other view controller. - Dhananjay Suresh

1 Answers

0
votes

The issue was that I was querying and modifying the RLMObject elsewhere in the setup of another view controller. This was causing the exception to be thrown. My fault for not looking closely at what my other code was doing.