Can Realm's executeTransaction be safely retried without duplicating writes?
20K reputation · 05 May 2022, 16:34 UTC
When a write operation fails inside Realm.executeTransaction(), the transaction is rolled back and the method returns false, allowing the caller to retry the same intent. Because Realm uses MVCC, readers see a consistent snapshot and committed changes are invisible until the transaction succeeds.
The goal is to confirm that a retry does not create duplicate objects, even if the retry occurs after a delay that may leave the MVCC snapshot stale. Considerations include whether a manual Realm.refresh() is required before retrying and how to structure the retry loop to guarantee idempotent writes.
Does Realm automatically refresh the MVCC snapshot on each retry, or must the caller invoke Realm.refresh() explicitly? What retry pattern ensures that application‑level logic does not inadvertently produce duplicate rows when using executeTransaction? Are there any scenarios where a delayed retry could still result in orphaned data despite the rollback mechanism?