Merge A Server Refresh Without Erasing Unsaved Edits

You are halfway through editing a title when a background refresh arrives. Replacing the editor’s model with the new server object discards your draft. Ignoring the refresh entirely keeps your draft but misses unrelated remote changes.

Keep three values: the base version the editor started from, the local draft, and the newly fetched remote version. That gives the merge enough information to distinguish a conflict from two independent edits.

Compare against the base

For one field, the rules are compact:

import Foundation

enum FieldMerge<Value: Equatable>: Equatable {
    case merged(Value)
    case conflict(local: Value, remote: Value)
}
func mergeField<Value: Equatable>(base: Value, local: Value, remote: Value) -> FieldMerge<Value> {
    if local == remote { return .merged(local) }
    if local == base { return .merged(remote) }
    if remote == base { return .merged(local) }
    return .conflict(local: local, remote: remote)
}

If both sides reached the same value, accept it. If only the remote side changed from base, accept remote. If only the local side changed, preserve local. If both changed differently, keep both as a conflict.

For example, start with title “Trip” and note “Pack light.” Locally you change the title to “Autumn trip,” while the server changes the note to “Bring a raincoat.” Merging those fields independently preserves both edits without asking the user to resolve a conflict that does not exist.

If both sides change the title differently, the algorithm returns the two alternatives. It does not pretend to know which wording the person intended.

Apply the result as one editor transition

Keep the base snapshot immutable for the comparison. Do not replace it with the new remote value before calculating the merge, or you lose the evidence of what changed.

After a conflict-free merge, the new remote snapshot becomes the new base and the merged result becomes the draft. Any remaining difference between draft and base is still unsaved local work.

When conflicts exist, retain the conflicting values and the associated remote revision until the person resolves them. Preserve their current typing, selection, and undo expectations. A background refresh should not move the insertion point merely because a model object was replaced.

If another remote update arrives during conflict resolution, define whether the editor recomputes against the latest revision or pauses refresh application until resolution completes. Silently changing the alternatives under the person’s finger makes a technically correct merge difficult to trust.

Field independence is a product decision

The generic helper works for Equatable values, including optional fields. It can distinguish a local deletion from a simultaneous remote edit.

It does not implement collaborative text editing, ordered-list merging, or a CRDT. Two changes inside one long String are still two different whole-field values. Use a domain-specific merge when that distinction matters.

Also consider related fields. A start date and end date may each merge cleanly while their combination becomes invalid. Validate the entire merged document before enabling save. Automatic field merging does not remove cross-field invariants.

Protect the write, not just the refresh

Even a correct merge can race another update after the fetch. Send the revision you merged against with the save. For an HTTP API using strong entity tags, If-Match allows the server to reject a write when the representation changed.

A failed precondition should fetch the latest representation and return to reconciliation. Do not respond by immediately retrying the same overwrite without the precondition. The check prevented a lost update; removing it defeats the protection.

The server must enforce the precondition atomically with the write. A client-only comparison before an unconditional request cannot prevent another writer from changing the resource in between.

Keep cache validation separate from this save precondition. A conditional GET asks whether your cached representation is current. A conditional write asks whether it is still safe to apply an edit based on that representation.

Test the conflict table

The companion covers local-only changes, remote-only changes, identical changes, different simultaneous changes, a local deletion, and a deletion-versus-edit conflict.

At the editor layer, add a test in which a refresh arrives while the title is dirty but another field is untouched. Assert that the title survives, the unrelated remote field updates, and saving uses the new remote revision.

September 2026 review: the generic merge assertions passed and the Swift example type-checked for iOS 17. The fixture has no live HTTP server; precondition enforcement and editor selection/undo behavior require integration tests against the actual app.