I am using parse for chat application in android for storing the data I am using both the server and local database(Parse.enableLocalDatastore()) it is working fine, the problem is with fetching data from the database like if network is not available the data will return from local database and if it is available it will return directly from parse so how can i differentiate between them? should i use query.fromLocalDatastore() method while quering the data or not?
You're going to have to build 2 different queries, one for local data and one for network data, and then figure out which one you want to display on screen.
There are a few scenarios you have to account for :
Server side deletion, addition and update
Client side deletion, addition and update
Not all of these need to be accounted for, only those that make sense for your application.
Keep in mind that when an object is pinned (and not saved to the server), it does not have an objectId, but it does have something called a localId (it's private api but you can see it in the debugger). You can check for the existence of objectId to determine if the object was created locally and has never been saved to the server.
Related
I am developing an Android app that has a list, I would like this list to be synced between multiple users - can it be done with out server side?
Syncing data between your webserver and an android app requires a couple of different components on your android device.
Persistent Storage:
This is how your phone actually stores the data it receives from the webserver. One possible method for accomplishing this is writing your own custom ContentProvider backed by a Sqlite database.
A ContentProvider defines a consistent interface to interact with your stored data. It could also allow other applications to interact with your data if you wanted. Behind your ContentProvider could be a Sqlite database, a Cache, or any arbitrary storage mechanism.
While I would certainly recommend using a ContentProvider with a Sqlite database you could use any java based storage mechanism you wanted.
Data Interchange Format:
This is the format you use to send the data between your webserver and your android app. The two most popular formats these days are XML and JSON. When choosing your format, you should think about what sort of serialization libraries are available. I know off-hand that there's a fantastic library for json serialization called gson: http://code.google.com/p/google-gson/, although I'm sure similar libraries exist for XML.
Synchronization Service
You'll want some sort of asynchronous task which can get new data from your server and refresh the mobile content to reflect the content of the server. You'll also want to notify the server whenever you make local changes to content and want to reflect those changes. Android provides the SyncAdapter pattern as a way to easily solve this pattern. You'll need to register user accounts, and then Android will perform lots of magic for you, and allow you to automatically sync. Here's a good tutorial: http://www.c99.org/2010/01/23/writing-an-android-sync-provider-part-1/
As for how you identify if the records are the same, typically you'll create items with a unique id which you store both on the android device and the server. You can use that to make sure you're referring to the same reference. Furthermore, you can store column attributes like "updated_at" to make sure that you're always getting the freshest data, or you don't accidentally write over newly written data..
I am developing an Android application to collect the store (Grocery) information.
The application have modules to create store, set it's attributes like address, lat lng, operating hours, manager details, building photos, etc.
Once the store is created user need to list down the assests of that store by clicking photos and providing it's details.
To store all this details, i have around 15 SQLite tables.
Now i want to implement feature of 'Synchronization', all this captured details need to send to server whenever connection is available otherwise detail should be stored locally and whenever connection is available it should move to server.
Also, please note that the number of tables may increase up to 40 as application grows.
I searched for the solutions/approaches for this on Google but in most of the article or example they have mentioned for small scale application having small data.
I have also implemented synchronization feature for small datatable (2 tables), where i checked for last updated timestamp on server and local and if it's different then we synchronize the data. I don't this i should use this approach for such large scale and large database.
I have one approach which doesn't depend on numbe of tables.
I am planning to have single table which store the following data
id
URL
request header
request body
Now let's say connection isn't available while sending request so it will be stored in table. Whenever connection is available it start reading the table and execute the request, on success it will remove the entry from table. With this approach we need only one table in SQLite.
The problem with this approach is when we want to retrieve data offline how we can do that? Do we need to have local database schema same as server?
Please guide.
Thanks
If you are syncing data with a server and you are removing local storage data ,which is incorrect as per my knowledge ,in this case your app does not work offline.So for that when you sync data to a server at that time maintain some flag which data is synced.And then next time just check flag status if it's synced then do not synced data otherwise do syncing.
I hope this solves your problem.
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.
I am developing an android application where it will be communicating with a server all the item. Specifically, the android interface has many fields were the user fills in the data and send them to the server (Glassfish with Oracle Backend). My concern is: what is the best way to store data when the connection is lost so that when it connects again, I can send the data to the server.
Note 1: Data are all textual and it can reach 1.5 MB in size. Also, there is a plan to save images too.
Note 2: I know about SQLite, but is this the best solution or there is sth else?
Finally, I would like to thank all of you for your collaboration
SQLite is a good solution.
Because your data size can be reach upto 1.5 MB you must store your data in an easy way, which you can easily retrive the stored data when tha connection is available to the server.
I also have used SQLite in android and i believe it will be the best resolution for your problem.
For more comparison see http://developer.android.com/guide/topics/data/data-storage.html
Use SQLite to save your data offline, clear the table if data is sent to server. Best method.
Use SharedPreference to save keys(sent Succesfully) and values(true/false).
- Ex Save Data in DB >> Send data to Server >> Get Acknowledgement (if failed, resend till success) >> Update keys >> delete data in DB >> Repeat cycle
Use Cache/local directories to save images
You can use an SQLite database, and have your rows include a Synced tag. If the sync fails, add a row to the database with Synced = False. When you later Sync the data and get a successful return message, you can update the row in the database to Synced = True (if you plan to have offline cached data) or simply remove the row if you're using the table as a temporary store.
You do not want to use SharedPreferences in this instance.
If you're going to use the database to keep a persistent store synced with the server online, you may wish to also look at the following:
ORMLite
GreenDAO
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?
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.
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.