Best strategies for sync when local data is modified multiple times - android

I've read a lot about regarding offline cache strategies but unfortunately I found nothing that can fulfils my requirements.
Let's say that I have a list of Order in my local database that need to be synced back with the server if one of them is modified. The Order can have multiple statuses like: New, Started, Paused and Completed. The status changes must be synced sequentially with the server because the order is also important.
Creating the table with "isModified" column is not good for me because i need to sync every status change one by one sequentially, not just the last one.
What are the best strategies to store all status changes when the device is offline? I am thinking about duplicating the Order table or creating a new one for the sync data because there can be also other objects not just Orders.
Any advice would be much appreciated.

If the order of modifications is relevant, you should store each modification in some kind of ordered data structure, e.g. another local table.

Use Firebase Database for offline database. It synchronises automatically when an internet connection is active.
Add the line below in your Application class:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);

Related

Keeping track of Deleted Entries between mobile Local Database and optional Cloud Database

I'm working on a Mobile App, where the main feature has the user do a lot of CRUD (Create, Read, Update and Delete) tasks within it.
The main storage of data for the App is a local sqlite database, but the user has the option to register an account and use Cloud database to backup their data.
This App needs to be able to work both offline and online. And the user should be able to use multiple devices containing the same data.
Currently in all of my sql tables I have 3 extra columns that keep track of which entries in the database is synced: createdAt(datetime), updatedAt(datetime) and synced(boolean).
With this I am able to keep track of which entries are the most recent, and update either the local or the cloud database accordingly.
I'm using Cloud Firestore as the Cloud, and using its' Offline capabilities is not a viable option in my case.
My Question is; Which solution would be the best when keeping track of deleted entries until the App is able to sync with the cloud?
One idea for a solution for this is to have an table that contains all the ID's of deleted entries together with which table it belongs, and then when the App is able to sync; remove these entries on both the local database(on all devices) and the cloud database.
The problem I have with this solution is that, this 'deletion' table quickly will become huge, and removing entries from this table would be a problem, because of the need of all the user's devices to be up-to-date before deletion and in a scenario where the user has abandoned one of his devices, this would mean that the device would never sync, resulting in the entries not being remove from the 'deletion' table.
What would your suggestion be for a robust way of tracking deleted entries?
I don't think there's a solution that satisfies both these objectives:
Don't keep deleted items in the database forever
Make sure deletions are synchronized between all devices forever
So you will have to decide which one to give up on. Your idea satisfies 2 but not 1. A solution that would satisfy 1 but not 2 is to delete the deletion records after a period of time, maybe six months. A variation on that would be when a record is deleted, rather than actually deleting it just mark it as deleted (as well as the date when it was deleted), and if applicable remove any large pieces of data from the record. After whatever grace period you decide, the record can be actually deleted. The down side is that if a mothballed device is brought back out, it could restore previously deleted records.

manage non local sqlite database in android

I have started working with databases lately, I was able to create a local database and manage it in my Android app. Now I want to move to higher level with it but I don't know how to do it.
The users in my app need to be able to modify the database, for example insert new data in it. When a user insert new data in the database I want other users to see this change in their copy of the database.
I understand that I will need to store that database on a server or something and synchronize it with the users.
Can anyone tell me the steps to do so?
You should perfom this task in steps.
First, make the local database, and use a system to know when/what changed.
I usually work with triggers myself, but any "mark" is enough to synchronize.
Then, you must make a replica of that database somewhere else. Realise that maintaining the databases is a process, any change in the structure of one database must be performed in all other as well.
Finally, you must implement a method to transfer the data.
So, for an example:
db_local the database in the device.
db_outside the database in the internet.
db_local.trigger -> onInsert
On the applications, check for internet, then connect to your server, then upload the same command to db_outside and run it...
In this step, you must handle connection issues, and if the SQL command was succesfully executed, you have replicated the database content.
Once you have the replicated database, inform a system (like google cloud messaging), that the database was changed, and have the other users pull the info.

Mobile DB app off line from internet

I have an architecture question. If you have a web app that is storing information on a DB server, theoretically, I should be able to use the middle tier logic for a mobile app. When the mobile app starts it can connect and populate a local SQLite DB or use JSON to store information within the mobile app. What if the mobile app also needs to work in off-line mode? Do you have it sync the next time it is connected? Do you have the mobile pull down and populate a complete DB or so it available in off-line? What are the best ways to architect a mobile app that has to go from on-line to off-line?
The simplest solution would be to put a "LastEdited" column into every table in your database and then pull query all the data which has updated since the last sync ( and you can perform a check on the index to detirmine if you need to update or insert into your own local cache. )
The ability to delete rows should actually be limited to a boolean "isDeleted" flag in this case to keep the sync process nice and simple.
If you have then the ability to edit or create rows from your app then you should keep a local table of changes to sync when you can go online and may have to implement some form of "merge" logic.
Several things you need to consider.
If your app is read only, you should implement a 'delta sync' logic in your local d. Keep a timestamp of last sync and get updates from your server. Of course, you need to consider the local db size in getting too large.
If you app is read/write, when working offline, you need to consider the two way sync especially when same record can be updated in different devices/users.

Should i keep a local copy of remote database?

I am working on an application that will, basically, allow people to create, join and manage groups of other people. The people within the groups can also message each other.
I have been wondering which path would be better:
Keep a remote database with all the information, including messages sent to and from users. And have the app query the server every time it needs information. Even information it has seen before.
Keep a remote database with all the information, including messages sent to and from users. Also keep a local copy of the remote database and just keep it synced with the remote database. Whenever the app needs to query for information, it does a query to see if the local table is up to date. If it is not up to date, it updates the table and runs the query on the local table. This way it will keep a local copy and the app will have fast queries when there is not an update to the remote table.
What is generally done with mobile applications and remote databases?
Would it be "bad practice" if i just did number 1?
From my point of view, in most cases, the database in the mobile is just a cache of the real database, the one in the server. So, my suggestion will be to keep locally all data that you need syncing with the server. This allows you to show information even when no connection and show something to the user while the info is updated.
Also, this approach makes the local data volatile without risk, as it's stored in the server. So:
All info is in the server
With a background process (service, thread, intentservice, whatever best suits you) you sync this information with the local database
UI is always showing info from local database
Of course, this is a very general approach, and needs to be examined for each case as different situations may need different approaches.
My base response is that I would keep the data in one place and access it remotely unless there is a major reason to keep it locally. There would have to be extenuating circumstances to mandate that I keep a copy of the data locally. Just make sure your queries are accurate and concise. Don't pull over more data than you need to.However, you can have a subset of data kept locally. Items that are specific to the user (like messages), but keeping data that is not relevant just adds overhead and bloat.

Android + Couchbase usecase?

I am currently developing an application that provides some temporary static, but often changing data.
You can think of it like a telephone book. Most of the entries might never change, but there might be cases where people change their telephone number or move in/out. The dataset isnt large but has to be available at any time.
My first thought was to implement a clientside-SQLDatabase which queries a backend in certain intervals asking for updates (stored in another database on the backend server). If there was an update since the last query, the updated data will be send over and stored in the local sqldatabase.
Now I stumbled over this interesting looking database model Couchbase. Altough I dont see through it completly by now, if I got it, I can use it to keep the client-side database in sync with the servers database. Is this a usecase for couchbase or should I go with another strategy?
Thanks in advance!
That's a perfect use case for Couchbase. You'll want to check out Couchbase Lite (https://github.com/couchbase/couchbase-lite-android) and the Sync Gateway (http://docs.couchbase.com/sync-gateway/).

Categories

Resources