Martin Lai
Martin Lai

Reputation: 165

What may be the reasons for "already in write transaction" errors for Realm iOS?

we are using Realm to cache ChatMessage objects within our iOS app. We also use Realm to persist some other local objects within our app. For chat messages, when we receive messages from PubNub, we create a new local ChatMessage object, and write it within the main queue. For other objects writes, we offload them into non-specific global queue and put the write transaction in it.

Currently, we are constantly experience exceptions when we wrote the ChatMessage objects, stating that the transactions were "already in write transaction". I checked all places to make sure there is no function that triggers beginWriteTransaction within each beginWriteTransacdtion/commitWriteTransaction codes. And the codes in between are mainly just Realm objects values modifications or creations. So we have absolutely no clue why this bug happened. (It's not very frequent. Usually 1-3 times per daily usage.)

Is it possible that multiple messages from PubNub come in (in main queue) triggers multiple write transactions leading to this issue? Or could some unfinished commitWriteTransaction cause this type of problem (we didn't check commit transaction errors yet, I'm not sure the best way to handle errors from Realm)? Also, could any write transaction failed when apps went into background, and causes such errors?

Thank you all very much!

Upvotes: 2

Views: 1978

Answers (1)

jpsim
jpsim

Reputation: 14409

StackOverflow is really not the best place for this kind of troubleshooting advice (emailing [email protected] lends itself better for that), but I'll give it a shot here anyway.

Realm's code for checking to see if you're already in a write transaction is dead simple, so odds are you're actually in a nested write transaction when you get this "already in write transaction" exception.

If you can reproduce this in a development environment, I'd suggest you set a symbolic breakpoint on all exceptions in Xcode and when this exception gets thrown, walk back up the stack to see where else you may have opened a write transaction that wasn't closed.

You could also add a breakpoint that logs the file & line at which a call to -[RLMRealm beginWriteTransaction] or -[RLMRealm writeTransactionWithBlock:] was made.

Upvotes: 1

Related Questions