user3840608
user3840608

Reputation: 27

insert huge data in sqlite database Android

I have a lot of data in json and i fetched it and get and set using getter setters and insert using below statement

long insertId = database.insert(StaticData.SCHOOL_TABLE_NAME, null,
                values);

but the query not inserted full record and my application crashed ,my no of rows are 40,000 .

Upvotes: 0

Views: 653

Answers (2)

Dharmesh Baldha
Dharmesh Baldha

Reputation: 816

According to you, the number of rows are huge so recommending that not to use AsyncTask, as it will not stick to Activity lifecycle. For Example, if the Activity dies, it doesn't mean AsyncTask dies as well,

1. But assume the rotation of the screen which occurred to restart Activity causes restart AsyncTask as well,

2. Once AsyncTask executed and back pressed and come back again into the same Activity before previously executed AsyncTask was not finished,

The behavior of both scenarios mentioned above may corrupt data or duplication of the data.

That's why I would Recommend using below approach,

Use IntentService and within the method named handleIntent() which executes in a worker thread so we don't need to worry about background task or blocked the main UI thread,

And another main advantage of using IntentService is, once operation of the insertion of the data into the SQLite Database will be finished, IntentService automatically self-kill that worker thread, so again no need to do manual stuff related to that,

And as it's running on a worker thread, there are very rare chances to worry about leaking from the memory.

Once insertion finished, we can do further operation related to UI using Handler and Messengers.

Upvotes: 0

Chintan Rathod
Chintan Rathod

Reputation: 26034

If you are inserting records in database on UI thread, I will suggest you to use AsyncTask for that task.

Too much work on UI thread might cause this problem.

Upvotes: 1

Related Questions