Offline data syncing similar to email apps

I’m working on an app that needs to handle offline edits and additions like email clients do. When users make changes without internet, the app should send those updates to the server once they’re back online. Here’s what I’m trying to figure out:

  1. My app already has a daily sync set up with a SyncAdapter.
  2. I want to add another sync for the offline changes.
  3. I’m not sure if I need to create a new authority for this second sync.
  4. If I do, does that mean I have to use different database tables?

Is there a way to have two separate sync operations that use the same database? I’m hoping to avoid duplicating data or complicating the database structure. Any tips on how to handle this would be great!

// Example of current sync setup
class MySyncAdapter(context: Context, autoInitialize: Boolean) : AbstractThreadedSyncAdapter(context, autoInitialize) {
    override fun onPerformSync(account: Account, extras: Bundle, authority: String, provider: ContentProviderClient, syncResult: SyncResult) {
        // Existing daily sync logic
    }
}

// What I'm trying to add
fun scheduleOfflineSync() {
    // How to set this up without conflicts?
}

Based on my experience, you can indeed handle both sync operations within your existing SyncAdapter without creating a new authority or modifying your database structure. Consider implementing a queue system for offline changes. When users make edits offline, store these changes in a local queue. Upon regaining connectivity, your SyncAdapter can process this queue alongside its regular sync duties. This approach maintains data integrity and simplifies your sync logic. You might want to prioritize the offline changes queue to ensure user edits are pushed first. Remember to handle potential conflicts that may arise when merging offline changes with server data.

hey there! i’ve dealt with similar stuff before. you don’t need a new authority for the offline sync. just use your existing SyncAdapter and add logic to check for offline changes. when internet’s back, trigger a sync manually. this way, you keep your current db structure. hope this helps!

I’ve tackled this issue in a few projects. Here’s what worked for me: implement a local queue for offline changes using a separate table in your existing database. When users make offline edits, add them to this queue table with timestamps. In your SyncAdapter’s onPerformSync method, first process the queue table, then do your regular daily sync. This way, you’re using one SyncAdapter for both tasks without needing a new authority.

To trigger syncs for offline changes, you can use a ConnectivityManager to detect when the device comes back online. When it does, call ContentResolver.requestSync() to start your SyncAdapter. This approach keeps your database structure clean and handles both sync types efficiently. Just make sure to implement proper conflict resolution for cases where offline and server data might clash.