---
title: "Design pattern for syncing data in android"  
description: "Design pattern for syncing data in android"  
author: "Anonymous User"  
published: 2015-11-23  
updated: 2023-07-02  
canonical: https://www.mindstick.com/forum/33622/design-pattern-for-syncing-data-in-android  
category: "android"  
tags: ["android", "android intent", "android libraries"]  
reading_time: 5 minutes  

---

# Design pattern for syncing data in android

I have been seeing two implementations for syncing [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) between the [server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over) and the [client](https://www.mindstick.com/articles/23198/3-steps-to-ensure-that-your-client-portal-is-impeccable) on [majority](https://yourviews.mindstick.com/view/81389/time-for-putin-s-popularity-led-by-majority) of the apps. This assumes no GCM is set up:-\
Running an [intent](https://www.mindstick.com/forum/33890/examples-of-intent-and-bundles) [service](https://www.mindstick.com/articles/105963/online-thesis-writing-service-for-college-kids-with-disabilities) periodically which downloads the data from the [network](https://www.mindstick.com/articles/13122/an-introduction-to-network-cables) and [stores](https://www.mindstick.com/forum/161350/how-does-sqlite-database-stores-tables) in the database.Implementing a [Sync](https://www.mindstick.com/forum/159443/sync-vs-async-exceptions-in-node-js) Adapter which runs periodically.Which of the above would you recommend to have in [your app](https://answers.mindstick.com/qa/33368/how-will-you-design-a-listview-which-downloads-a-lot-of-images-and-displays-in-it-without-getting-your-app-hanged-whats-the-best-possible-way) and why?

## Replies

### Reply by Aryan Kumar

There are a few design patterns that can be used for syncing data in Android.

One pattern is the **Observer** pattern. In this pattern, the data source is the **subject** and the clients that are interested in the data are the **observers**. When the data source changes, it notifies the observers, which then update their own data accordingly.

Another pattern is the **Memento** pattern. In this pattern, the data source creates a snapshot of its state at a particular point in time. This snapshot is called a **memento**. The memento can then be saved to a persistent storage or sent to another client. When the client needs to restore the data source to a previous state, it can use the memento to do so.

A third pattern is the **Command** pattern. In this pattern, the data source defines a set of commands that can be used to modify the data. The clients can then send these commands to the data source to update the data.

The **Singleton** pattern can also be used for syncing data. In this pattern, there is only one instance of the data source. This instance is responsible for syncing the data with the other clients.

The **Factory** pattern can also be used for syncing data. In this pattern, there is a factory class that creates instances of the data source. The clients can then request an instance of the data source from the factory class.

The best design pattern to use for syncing data in Android depends on the specific needs of the application. If the data source is simple and only needs to be synced with a few clients, the **Observer** pattern may be a good choice. If the data source is complex or needs to be synced with a large number of clients, the **Memento** pattern or the **Command** pattern may be a better choice. If the data source needs to be synchronized with other devices, the **Singleton** pattern or the **Factory** pattern may be a good choice.

### Reply by Anonymous User

Sync adapters run asynchronously, so you should use them with the expectation that they transfer data regularly and efficiently, but not instantaneously. If you need to do real-time data transfer, you should do it in an AsyncTask or an IntentService. - source.\
Basically, if you need real time transfer use IntentService (the first option), else SyncAdapter. I prefer an IntentService though because it feels more customizable, but a more trivial approach would be to use a SyncAdapter.\
It heavily depends on what kind of syncing you need.**\****Periodic**\
If your app is a news app that publishes posts at a certain time every day(lets say at 7.45 AM every day), then you run a periodic task in a background service, say at 8 AM.\
e.g.: Drippler. They notify me once every day(around 6.30 PM). I believe they use a periodic task.\
**Event Triggered**\
If your data transfer is triggered by user action, then use a background service or an AsyncTask for the data transfer.\
e.g.: DropBox/Evernote. They sync when I interact with the app.\
**Instantaneous**\
If your app runs instant messaging/mails/non-periodic important updates, then you need push notifications, because you want to alert the user immediately. Use either GCM or Parse for this case. e.g: WhatsApp/Google chat. Since you explicitly mentioned you don't want to use GCM, I will tell why you should use a standard push notification provider instead of writing your own:\
Push notifications work instantaneously - there is very little delay(in the order of seconds, rarely minutes). If you were to implement your own solution/library for doing this - in a naive model, you would ping the server every second or 5 seconds or a minute to check for the status. This is very inefficient as it consumes CPU(and hence battery), bandwidth and load on your server. However, in GCM/Parse, they always keep a port open with the server(see here). This is the standard and most efficient way. Plus, if 10 apps use GCM, you don't need 10 open connections, you only need one per device. And you really don't want to develop your own solution unless you have a valid reason/funds/time to do so.\
A note on Sync Adapter: Sync Adapter works well for all the above three cases. Check Running a Sync Adapter and you will see that it either depends on GCM or your own mechanism(event trigger or custom solution) or network availability(event trigger) or periodic event. All in all, this is a good convenient class for syncing data without having to do a long list of initializations every time or to implement all the above cases at one place


---

Original Source: https://www.mindstick.com/forum/33622/design-pattern-for-syncing-data-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
