Squadrons
Squadrons

Reputation: 2557

Android: Best practices for storing data while offline when data is stored in online DB

I'd like to make a basic to do app in android to get my feet wet. I have a rest api and online DB that handles the basic CRUD when there is a connection present.

Most task apps I've used however, allow creating tasks when there is no connection present.

What are the best practices for stuff like this?

Do apps usually store a copy of ALL data for a user locally so there is access to it when a connection is not present?

It looks like the app I use (astrid tasks) has no problem accessing all my tasks/history regardless of connection

If this is the case, how is syncing handled as far as the remote data's primary keys are concerned?

Upvotes: 3

Views: 2226

Answers (2)

Asaf Sh.
Asaf Sh.

Reputation: 49

You have some encoding, let say one request per single data change to be executed atomically encoded as xml or json. Make a base class which is parent of connection and use it to send data update to remote db. If connection isn't present store entire command into file or sqlite. You can create multiple files (if going by file approach) based on their sizes, date etc. Create some rules how the oldest record will be chosen - if you need to update db in ordered manner.

Upvotes: 1

Andy Res
Andy Res

Reputation: 16043

One solution would be to have a local database in your application. When there's no internet connection store the data in this database.

Now let your application listen for network changes. When the device is connected to the network, you could upload the cached data from local database to the server without user interaction.

Upvotes: 0

Related Questions