Does Realm Offer a Built‑In Retry Wrapper to Prevent Duplicate Writes?
0 reputation · 29 May 2020, 04:37 UTC
0 reputation · 29 May 2020, 04:37 UTC
Goal: Safely retry a Realm write after a transient failure without creating duplicate persisted records.
Constraints: Realm aborts the transaction on error, primary‑key upserts prevent local duplicates, but Realm Sync may still record separate change entries and transaction‑abort guarantees differ between Core versions and language bindings (Java, Swift, Kotlin, .NET).
Uncertainty: It is unclear whether Realm should provide a built‑in retry wrapper that transparently suppresses write attempts that would not alter the persisted state, or leave this logic to application developers.
Does Realm currently expose a mechanism to automatically deduplicate retried writes?
Which language bindings guarantee transaction abort on all error types?
Should developers rely on primary‑key upsert behavior or implement their own retry wrapper?
35275 reputation · 29 May 2020, 13:08 UTC
Realm does not expose a built-in retry wrapper that automatically suppresses or deduplicates retried writes. Retry and idempotency logic is the application’s responsibility.
realm.write, Realm.executeTransaction, realm.Write, etc., with no built-in deduplication layer.insertOrUpdate, copyToRealmOrUpdate, realm.add(obj, UpdatePolicy.ALL) will update an existing object with the same primary key instead of creating a second row.Local state safety does not equal sync history safety. Realm Sync uses operational-transform conflict resolution. A retried write that reaches the server after a transient network error will generate a new operation in the sync history even when the final merged state is identical. Primary-key upserts keep the object state correct locally and on the server, but they do not prevent separate change-set entries.
In offline-first scenarios a write that succeeds locally but fails to sync before app termination will be re-sent on next connect. The server will apply it again; the state remains correct if the primary key already exists, but sync history inflates.
Assumption: you are on a current Realm Core / SDK release where exceptions abort the transaction. Older JavaScript/React Native versions surfaced sync errors via callbacks rather than exceptions; verify the specific binding’s error-propagation model for your version.
One diagnostic detail that changes the recommendation: Are you using Realm Sync with Atlas Device Sync or a local-only Realm? Sync requires application-level idempotency for history size; local-only relies on primary-key upserts and transaction rollback.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.