Skip to main content

Mobile Offline Sync with Conflict Resolution

Implement offline-first data synchronization for mobile apps with conflict detection, resolution strategies, and background sync.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
You are a mobile data synchronization expert. Help me implement offline-first sync for a field service management app that must work reliably without internet connectivity.

Step 1: Design the local database schema using SQLite via Room (Android) / Core Data (iOS). Mirror the server-side 8 entities with additional sync metadata columns: local_id, server_id, sync_status (synced, pending_create, pending_update, pending_delete, conflict), last_synced_at, and version number. Create a sync_queue table that logs every local change with: entity type, entity ID, operation (create/update/delete), changed fields, timestamp, and retry count.

Step 2: Implement the change tracking layer. Wrap all local database writes in a repository pattern that automatically logs changes to the sync queue. For updates, store both the old and new field values (needed for conflict detection). For creates, generate a client-side UUID that the server will acknowledge. For deletes, use soft-delete locally until the server confirms. Ensure all operations are atomic within SQLite via Room (Android) / Core Data (iOS) transactions.

Step 3: Build the sync engine that processes the sync queue when connectivity is available. Implement a push-then-pull incremental sync strategy: push local changes to the server API at /api/v1/sync, then pull remote changes since the last sync timestamp. Handle pagination for large change sets (over 100 records). Prioritize syncing work orders entities first. Run sync in a background process using WorkManager (Android) / BGTaskScheduler (iOS) to avoid blocking the UI.

Step 4: Implement conflict detection and resolution. A conflict occurs when the same record is modified both locally and remotely between syncs. Detect conflicts by comparing version numbers. Implement 4 resolution strategies: last-write-wins (automatic, based on timestamp), server-wins (automatic, server version always takes precedence), client-wins (automatic, local version preserved), and manual merge (present both versions to the user in a side-by-side diff view interface). Allow configuring the default strategy per entity type.

Step 5: Handle edge cases: sync interrupted by network loss (resume from last successful item), app killed during sync (recover queue state on restart), clock skew between client and server (use server-assigned timestamps), and data schema changes between app versions (run migrations before sync). Implement an exponential backoff retry with max 5 attempts for failed sync operations.

Step 6: Build the sync status UI. Show a persistent indicator of sync state: fully synced (green), syncing in progress (animated), pending changes (yellow with count), and sync error (red with retry action). Let users manually trigger sync, view pending changes, and resolve conflicts. Track sync metrics: average sync duration, conflict frequency, data freshness, and queue depth. Report metrics to Firebase Analytics.

What this prompt does

This prompt implements offline-first synchronization for a mobile app that must work without connectivity, including the conflict handling that teams usually bolt on too late. It moves through six steps: designing the local schema with sync metadata, building a change-tracking layer, implementing the sync engine, adding conflict detection and resolution, handling edge cases, and building sync-status UI. Forcing the metadata and conflict strategy into the schema from day one is what prevents data corruption later, after the data has already diverged.

The variables shape the system. [local_database] and [entity_count] define the mirrored schema, [sync_strategy] and [api_endpoint] plus [batch_size] drive the sync engine, and [priority_entity] decides what syncs first. [conflict_count] and [merge_ui] configure resolution, [background_service] runs sync off the UI thread, [max_retries] bounds retries, and [analytics_tool] receives sync metrics. The hardest value to get right is the conflict default per entity, since automatic strategies can quietly lose data.

When to use it

  • You are building an app that must remain fully usable without internet.
  • You need a local schema that carries sync metadata from the start, not added later.
  • You want a change-tracking layer that logs every local write to a sync queue.
  • You need real conflict detection when the same record changes locally and remotely.
  • You want the hard edge cases (app killed mid-sync, clock skew) handled deliberately.
  • You need a sync-status UI so users can see and resolve pending changes.

Example output

Expect a [local_database] schema mirroring [entity_count] entities with sync_status, version, local_id, and server_id columns, plus a sync_queue table logging every change. The middle steps produce a repository layer that records writes, a sync engine implementing [sync_strategy] against [api_endpoint] with pagination beyond [batch_size], and [conflict_count] resolution strategies including a [merge_ui] manual merge. Edge-case handling with exponential backoff up to [max_retries] and a sync-status UI with synced, syncing, pending, and error indicators round it out. It blends schema, code, and UI specs, with the sync metadata columns treated as first-class parts of the data model rather than afterthoughts.

Pro tips

  • Put sync metadata (local_id, server_id, sync_status, version) into the schema for all [entity_count] entities up front; retrofitting it after corruption appears is painful.
  • Store both old and new values on updates so conflict detection by version number actually works.
  • Pick [sync_strategy] and [batch_size] to match your data volume; large change sets need pagination to avoid timeouts.
  • Choose a default among the [conflict_count] strategies per entity type; last-write-wins is convenient but silently loses data in some cases.
  • Don't skip the edge-case step; app-killed-mid-sync and clock skew are exactly where offline sync breaks in production.
  • Use server-assigned timestamps to neutralize client clock skew rather than trusting device time, which drifts between devices.

Frequently Asked Questions

How does it detect conflicts?
Each record carries a `version` number, and a conflict is flagged when the same record changed both locally and remotely between syncs. Storing old and new field values on updates lets the engine compare versions accurately rather than guessing from timestamps alone.
What conflict resolution strategies are available?
Step 4 implements `[conflict_count]` strategies: last-write-wins, server-wins, client-wins, and manual merge presented in a `[merge_ui]` interface. You can set a default per entity type, which matters because automatic strategies like last-write-wins can silently discard valid changes.
Does it handle the app being killed during a sync?
Yes. Step 5 covers recovering the queue state on restart, resuming from the last successful item after network loss, and using server-assigned timestamps to handle clock skew. These edge cases are the part most implementations skip and later regret.
Will sync block the UI?
No. Sync runs in a background process using `[background_service]`, processing the queue when connectivity is available. A sync-status UI shows synced, in-progress, pending, and error states, and lets users trigger sync or resolve conflicts manually.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Mobile App Development Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support