Reputation: 1437
I am new to Realm (using the Xamarin version for Android development in Visual Studio 2015) and want to know the best practices for handling transactions.
Since Realm requires all writes to a RealmObject's fields to happen in a transaction, if you decide to data-bind to such an object so end-users can edit it via your UI, you have to keep an open transaction until the user hits Save...which can happen in a few seconds, or a long-time later.
Realm uses MVCC to manage concurrency so reads never block and writes only block other writes (that's my understanding) so keeping a long-running transaction around, although a bad idea (at least for those of us used to working with SQL Server), doesn't seem to be so bad in Realm if reads don't block. But it still feels wrong to me.
The related questions:
1) what is the recommended way to do data-binding to RealmObjects?
2) is keeping a transaction open in any UI context where it can stay open for unpredictable amounts of time a potential source of bugs or poor performance?
Thanks in advance (this is also my first question on SO - hope I haven't stepped outside the guidelines!).
Upvotes: 2
Views: 367
Reputation: 919
Yeah, this feels wrong to us too :-)
We have an improvement in mind (https://github.com/realm/realm-dotnet/issues/647), so that you will not have to resort to this.
Regarding your other questions:
1) this blog post goes into a bit of detail, I don't know if that helps you. If you have more specific questions don't hesitate to ask.
2) Long lived transactions are an antipattern for a reason, in Realm as well as in SQL databases. The updated state is not persisted until the transaction is committed, so your user might lose work if the transaction represents a lot of changes and your app crashes. Furthermore, other threads that attempt to read will access the old version and if they attempt to write, they will be blocked until the transaction is committed.
Upvotes: 1