Android SQLITE only written when debugging - android

I am currently using an implementation of IntentService to fetch JSON from the network, parse it and insert into a database using a ContentProvider. I am getting some very inconsistent results. When I run the application nothing gets inserted into the db, but when I debug all the records get inserted properly. When running in real time I am writing the parsed JSON to the Log and it shows up fine. It just won't write to the db without being in the debugger. Has anyone experienced an issue like this?

Originally I had 2 IntentServices running, one to fetch some JSON and insert it into the database and the other to fetch some large images. Originally I thought it would be best to start them both at the same time, it appears this was a bad idea. As soon as I disabled the image fetching service all the database writes and reads have been working fine.
This solves my original problem but doesn't answer any of the questions for me like how many separate services can run at once or is SQlite really thread safe. Anyways, if someone happens uppon this that was my solution.

Related

CachedRowSet on Android?

there is any way to use a CachedRowSet or similar on Android? I'd like to save my ResultSet's values into any object independent of the Statement, and then close my Statement without losing the information that I've retrieved.
I'm using jdbc MySQL 5.1.27 connector.
Thanks in advance.
My main problem is that in some methods I'm getting various ResultSets and while I'm working with them I can see how my tablet is working slower. I want to close every Statement I've opened and keep working with the ResultSets, but the problem is that the ResultSets are dependant of the Statemenents, so if I close the Statement, I'm losing my ResultSet.
I want to use something like 'CachedRowSet', what is supossed to work like a ResultSet but without having to stay connected to the database.
I use JDBC cause the app works in closed networks, retrieving information directly from the server.

Android app taking much time to load data from sqlite

I am making an android app which requires 1200 records to insert and then fetch. I am using sqlite as back end. Everything is working fine but app takes lot of time to load the data at very first time for new installation. I am using SQLiteOpenHelper class to insert data. Please suggest ways to reduce the time to load the data at very first time.
EDIT :
I resolved my problem:) Here is how?
My app was taking much time because, I was inserting all the 1200 records at a time. Now i changed my code such that it should insert only required data one at a time so it saved my lot of time.
Use transactions as explained in this answer and make sure that you are only creating one connection to the SQL database for the transaction(s)
There should be a significant speed increase

Is SQLite appropriate for off-line storage before replication to a server?

I am planning on writing an application that saves a fair amount of data. Historically, I have simply written data directly to a server, and only used some simple key/value storage with shared preferences for local storage.
I am considering this time, instead, using SQLite to save the information at first, and sync the data to the server in the background later. This will benefit the user in a few ways: 1) can use the app offline 2) don't have to worry about data being saved right away, it happens when ever it can 3) more reliability.
My approach will be to get/set data from SQLite during UI usage, and use a background process to find new rows and put them on the server, flagging them as synced when it happens.
Does this sound reasonable?
You can use SQLIte for your scenario. But, while implementing, you can follow any one of this approach.
Approach #1: Use an Abstract Factory to Instantiate the SQLiteOpenHelper.
Approach #2: Wrap the SQLiteDatabase in a ContentProvider
Refer to this link for how to implement these 2 approaches. http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html
Key points to be noted while using SQLite
Sqlite takes care of the file level locking.
Many threads can read,one can write. The locks prevent more than one
writing.
Android implements some java locking in SQLiteDatabase to help keep
things straight.
If we handle the database incorrectly from many threads and mess up the code, your
database will not be corrupted. Only few updates will be lost.
How "Multiple Threads - DB access" can be used for your scenario
The SqliteOpenHelper object holds on to one database connection.
If you try to write to the database from actual distinct connections (multiple threads) at the same time, one will fail. It will not wait till the first is done and then write. It will simply not write your change. Worse, if you don’t call the right version of insert/update on the SQLiteDatabase, you won’t get an exception. You’ll just get a message in your LogCat, and that will be it.
So recommended to write using single thread and read from multiple threads if necessary for faster access.
Does this sound reasonable?
Yes. Note that the synchronization process can get tricky (e.g., what happens if the server hiccups halfway through?), but that has mostly to do with synchronization and little to do with SQLite.
We implemented a solution that used a SQLite db on the device to sync data via a web service to the master database. We did this for a couple reasons: offline, poor connection, manual sync.
For our solution we had a flag on the table that determined if the data was pushed to the web service. Our web service also provided data back to our application to let us know if the data was received and processed correctly. This allowed us to clean up the data on the device, send notifications if there were failures, and resubmit the data if there were previous failures.
You can use push notifications as well if you have fixed the issues on the backend and have the device resend the data to the web service. This worked really well for us.

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.

How to avoid SQLiteException locking errors

I'm developing an Android application. It has multiple threads reading from and writing to the Android SQLite database. I am receiving the following error:
SQLiteException: error code 5: database is locked
I understand the SQLite locks the entire db on inserting/updating, but these errors only seem to happen when inserting/updating while I'm running a select query. The select query returns a cursor which is being left open quite a wile (a few seconds some times) while I iterate over it. If the select query is not running, I never get the locks. I'm surprised that the select could lock the db. Is this possible, or is something else going on?
What's the best way to avoid such locks?
You are probably opening and closing multiple database connections in your various threads. This is a bad idea. Just open a single database connection, and reuse it everywhere; SQLite will then ensure that concurrent accesses are serialized correctly.
As with jcwenger's answer, using a ContentProvider is another way of achieving this, but will require much more intrusive changes to your code.
By avoiding leaving cursors open for "quite a while". If you can afford to have all your data in memory all at once, then do so.
If you can't, then try increasing the busy timeout.
Migrate to a ContentProvider rather than directly accessing the DB. ContentResolver marshals away all the threading issues for you and also allows for other useful features like sharing data between apps or syncing with a server.
The api overhead of ContentResolver is minimal. You just need to define an AUTHORITY string (A unique string identifying the "kind" of your data -- use a "com.example.myapp.contacts" type of string) and use ContentResolver.bla rather than db.bla.
Its caused by beginTransaction() function.Look at your code, the problem is solved for my app to making a comment line this function(beginTransaction) line

Categories

Resources