How to perform synchronization in android? - android

I referred so many links regarding synchronization
http://developer.android.com/training/sync-adapters/index.html
my requirement is to download all the data regarding the user at first time.There are nearly 12 tables are existed at the service i need to download all the 12 table into the android mobile at first time. After that we will modify that local data whenever we click on that sync button we need to send that updated data to the server.
I am following this type of approach: I am sending a json object to android mobile from server side which contains all the 12 tables data.by using that json object i create the tables and insert those data into the local database.Is this correct approach?
Is there any jars exists to simplify this type of requirements?
Please give any suggestions regarding this question.

For downloading the data. You can use AsyncTask in Android. Its runs at background so no worry if you are changing the UI. Here is the documentation
AsyncTask
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
Here is the good tutorial on this topic have a look on this
Android Background Processing with Handlers and AsyncTask and Loaders - Tutorial

Related

Fetch live data from server

I need share some data between Android devices live (at least every 5 seconds update). I first had in mind to create an async task in which the one device sends its own data to a server and gets the other data as response. I recently read about firebase or synchronized database so I was wondering which is the best way with keeping performance in mind.
I'm not asking for code but for ideas to improve my app. I also need to save the "old" data from each client for a history.
There is no need to use AsyncTask
For implementing Firebase operations. Firebase is already optimized. So you don't need to use a background thread for performing network-related operations using firebase.
firebase childevent listener frequently checks for database changes and you can show them on the UI screen, no async task needed.

How to download data from Firebase in Android efficiently?

After doing some research it is still not clear to me how exactly Android downloads data from Firebase. Android has a lot of best practices for performance such as using an AsyncTask or Volley, but I need to understand how Firebase operates before I can make decisions about them.
To be more specific, does Firebase load data in a separate thread? When I am downloading profiles (a profile picture with some text) in a FirebaseRecyclerAdapter I would like to download the text for each profile first and the pictures in a separate thread. I know how to do this when downloading data from the internet normally but I do not know what Firebase already does.
Firebase uses a single separate thread for all its (network, disk, etc) operations. But your callbacks will always be invoked on the main thread, so that you can safely interact with the UI.
But if you perform any non-trivial operations in the callback, it is (as usual) your job to perform those operations off the main thread. So a AsyncTask or IntentService are still the proper approach there.

Fetching data from server (mysql databse) to android application and saving it into sqlite database

I am working on a project in which i have to get data tables present in mysql database on server. Now i have to insert that tables in my android application.
I had successfully achieve that functionality using Json Parsing and asynctask and sqlitedatabase, I have written php code by which i am fetching data from mysql server to my application.
Problem in this approach :
Time : As i am fetching that data from server in my Launcher Activity, My activity starts and take about 10 min to get all data from server. I want to reduce that time, I dont want that much delay, 5-10 sec will be fine
Everytime my application starts it goes in that fetching mode. which result in 10 min of loading page.
I dont want this, I want something that checks for the changes in database present on server, if there is a change then it should start fetching on background itself. I am thiking of service with alarm manager , but i dont know how to achieve all that with service. Should i use asyncTask in service or something else.
I am not sure if that detail is sufficient or not but i will give you detail explanation if needed. Any help in this will be appreciated, If my approach is wrong then I would be more then happy to change my approach to an optimized way.
Use Java threads instead of AsyncTask.In thread you could not access the UI.But performance point of view java thread is efficient for downloading large data.
Here some benefits of thread:
Network operations which involve moderate to large amounts of data
(either uploading or downloading)
High-CPU tasks which need to be run
in the background.

concurrent sqlite access in android application

My application has :
Activity A that reads from sqlite database
Service with notification that writes to the database
on clicking Notification, Activity A opens up
the reading by ActivityA is very small task(in reference to time taken to read)
but the writing by the service to the database is very long(it sometimes takes 5-10min)
now when the service is running and i click on the notification, ActivityA that has to read from the database cannot perform its reading as there is already a service writing to that database.
so activityA has to wait (for 5-10min) to read from database.
on researching further i came across this
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#beginTransactionNonExclusive()
when i try to implement this in my method inside sqliteopenhelper class i get error as my application uses min api 10. so how do i get this method working for api 10 or is there anyother way to have parallel database access
?
is there anyother way to have parallel database access ?
I think there is no special way how to achieve it. You should use classic Java synchronization for synchronized access to your database.
Most important thing is that you have to make sure you have only one connection to database (you can't write/read from two different connections in the same time). And try to think about an usage of Singleton. In this case (and also in others) it's very efficient and clean solution and you can avoid many problems with access to db.
You mentioned that your task can last 5-10 min.
In similar cases every user should know that you are performing some calculations in the background e.q. show some progressDialog, progressBar or simply start animation of image.
If you are showing some data for example in List this is good reason to use lazy loading.
Have look also at these articles:
Android Sqlite Locking
Using Singleton design pattern for SQLiteDatabase

How do I make an Android background thread?

I have an Android application that connects to a database. I would like to write a background thread to continuously update data from the database, using httpclient, and still be able to update the UI displaying the most recent data, let's say, every 1/2 second. How do I do that?
Have a look at AsyncTask: http://developer.android.com/reference/android/os/AsyncTask.html - this is exactly what you need.
That's a lot of data!
If you are displaying and then inserting the same data into the database then AsyncTask will work. However, if you are planning to access your database from the UI at the same time as inserting data from online then you will run into concurrency issues.
I had to use a content provider to provide concurrent access to the database. The beauty of that was that I could then use a syncadapter to handle the data retrival without affecting the UI.
You Can use the class AsyncTask , refer this tutorials to know more about it :) :
http://www.android-ios-tutorials.com/182/show-progressbar-while-downloading-image-using-asynctask-in-android/ ,
AsyncTask Official Documentation

Categories

Resources