is there any delay time to insert data using Ormlite - android

I have an Android app and in my login activity I get my data from a webservice and insert it into sqlite database and in my main activity I select this data from the database.
My problem is the first time I use use the app the list will be empty
I debugged the app and when I keep small amount of time between the login and the main activity , it works well but when I run it fast the list will be empty
I supposed that there is a delay from Ormlite library , I don't know maybe I am wrong
How can I solve this problem ?
Note: I used Async task to read data from the web-service and in the post execute method I inserted the data into the database then I called my main activity using intent

I supposed that there is a delay from Ormlite library , I don't know maybe I am wrong
The amount of time that it will take ORMLite to update the database is directly proportional to the amount of data being changed. If you are adding a large number of rows then you should consider doing it as a batch using the Dao.callBatchTasks(...) method.
See here for more details: ORMLite's createOrUpdate seems slow - what is normal speed?

DB operations especially multiple one take there time. That is also somewhere stated in the ORM documentation. So it is the right way to use AsyncTask to do this. I would argue to fill the database not on the post execute method but in the doInBackground method like your WebService call. Ensure to use a WeakReference to the ORM-DB-Connector. Furthermore, give the user a hint about the progress using a combination of Handler and ProgressDialog.

Related

Setting initial values for SQLite in onCreate() of SQLiteOpenHelper

I am creating an Android app for which I need to create a SQLite DB and pre-populate it with some values.
The Android documentation says this about what to do in "onCreate" of the SQLiteOpenHelper:
Called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen.
Reference - http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#onCreate(android.database.sqlite.SQLiteDatabase)
I am doubtful about the following 2 things -
What is meant by "when database is created for the first time"? Is this done on the first launch of the app or only when the first DB request (read/write etc) is done.
If it is the latter, I fear that it may take quite some time to create DB, pre-populate it with values (I have about 60 rows to be inserted into 1 table) and then read the DB to show it. Is this the best practice?
I have been doing all my DB operations in AsyncTasks. But I am doing the table creations in onCreate using "db.execSQL" statements. Is this fine (in terms of convention/ performance) or should I go for an AsyncTask here as well?
Any help is appreciated.
1) The later. It is done on the first read or write to the DB.
Your fear might be correct, this is why you can ship your app with a database that's already populated. Or you can launch an AsyncTask with a simple SELECT 1 FROM anytable query. More about shipping with DB here. (60 rows is nothing to fear about tho, and you can safely just keep using AsyncTasks).
2) Yes it is fine. The onCreate logic will run when you first read/write the DB, so it if you always use AsyncTasks onCreate will run in an AsyncTask also.
What is meant by "when database is created for the first time"? Is this done on the first launch of the app or only when the first DB request (read/write etc) is done.
It happens when you first query from database in general term. After that only Upgrade method is called that too when you change the db version.
If it is the latter, I fear that it may take quite some time to create DB, pre-populate it with values (I have about 60 rows to be inserted into 1 table) and then read the DB to show it. Is this the best practice?
60 rows insertion is not a big task. More you can read about beginTransaction(),commitTransaction and endTransaction for insertion. It will make your insertion task lighting fast.
I have been doing all my DB operations in AsyncTasks. But I am doing the table creations in onCreate using "db.execSQL" statements. Is this fine (in terms of convention/ performance) or should I go for an AsyncTask here as well?
It good you are doing you Db operation in AsyncTask and its completely fine.
Speaking of DB operations:
Performing DB operations in AsyncTask is not a good approach, generally. As you might encounter a problem called "memory leak", and it might come as a silent assassin in the night.
There's lot written on this issue. Just google "asynctask leak context" and here you go.
So how to perform DB operations?
Using Loader API in conjunction with ContentProvider is considered good approach for querying database. Loader asynchronously queries your database and delivers the result to specified subscribers. Configuration changes or other sudden stuff does not bother it.
And it is really convenient to query your data using loader API once you know how to do it.
Single inserts/updates/deletes might be done directly from the main thread via ContentResolver. These calls will be blocking (synchronous), but I bet you user would never notice anything while the amount of data is not large.
If you're operating on a large dataset, and you fear you'll be significantly blocking UI thread, I'd suggest using IntentService or any custom Service capable of doing operations in background (note that by default Service operates on main UI thread and you have to specify background operation yourself or use IntentService)
Speaking of DB initialisation:
You might create a one-time IntentService, if you're initialising a large set of data. It will handle your request asynchronously and, for example, perform a broadcast that the application is set up and ready, so you might stop a "wait a sec, performing app initialisation" screen and show user your data.
There's also nothing wrong with shipping your database along with application, though it appears to be a bit hackish solution.
Either way, you choose what is more suitable for you.

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

Activity won't start until data is pulled from MySQL server

I have an Activity that displays a text based on data pulled from MySQL server. The problem is that the Activity won't load until data is pulled, which sometimes takes some long seconds or even doesn't load at all, and in the meantime the users gets a black screen.
I tried to pass the mission of getting the data from the server to a service, but also it waits for pulling the data and only then shows the layout of the Activity.
I also tried to make an activity with fixed text and then call the Activity that pulls the data from the server, but still the program wait for the data.
Can you think on a creative solution for it? or maybe a non-creative one as well :)
you can use asynctask for this:
http://developer.android.com/reference/android/os/AsyncTask.html
or you can show a waiting dialog to user until you get your data(do it in separate thread).....
or you can implement a splash screen and there you can fetch data.....
You need to do it inside another thread. Try using AsyncTask class.
The delay is probably due to the call to fetch the data being done on the main thread, also called the UI thread. Processes which take any significant amount of time, and by that I mean even a second or two should be done in a seperate thread. Android provides a class called AsyncTask to help make threading painless.
You mention you tried a service but did you take a look at an IntentService? (Can't link it yet but it's on d.android.com.) I like using them for these kind of tasks cause they handle the threading for you (like an AsyncTask) and it separates concerns better. The IntentService then sends a broadcast message that the activity picks up indicating that the data is available or not. Store the data locally in a sqlite db or as a json/xml file.

Categories

Resources