Reputation: 831
I worked in android app that read large text file from server (20 MB) line by line , After each line insert into sqlite DB some data extracted from this line and so on .
the problem is, this application takes one hour approximately to finish reading this file . note : there are 400,000 line in this file .
how could i fast this operation to take 3 or 4 minutes ? Thanks for your help .
public void readArData() {
try {
URL url = new URL("http://10.0.2.2/xy.txt");
BufferedReader in = new BufferedReader(new
inputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
// insert into Sqlite DB
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
}
Upvotes: 1
Views: 2262
Reputation: 10193
Text files are highly compressible so use the gzip compression available on Android.
Other options include:
splitting your dataset into multiple files and again using gzip download them separately
seeding the database during the install and creating a synchronization method to update newer records and bring the mobile version up to date.\
remove redundant data using an efficient database design and only downloading what's required
UPDATE
To clarify:
You should now have a copy of your txt file on local storage so you can delete the local zip file.
The major differences in this approach are
a. that you are downloading less data b. your download is not interrupted by sql inserts.
You can also try using a transaction to improve insert speed as described in Android SQLite database: slow insertion
db.beginTransaction();
for (entry : listOfEntries) {
db.insert(entry);
}
db.setTransactionSuccessful();
db.endTransaction();
Upvotes: 1
Reputation: 11441
this approach will also allow you to resume an interrupted download, which is not bad.
Upvotes: 0
Reputation: 4514
Are you sure 3 - 4 minutes is an achievable amount of time? I ask because you have to:
1) Download the file contents 2) Parse the file contents 3) Insert into a database
Some of these operations can occur simultaneously but #1 is highly dependent on the network speed and 2/3 are highly dependent on what sort of system resources you have available.
Instead, can we ask why you need to pull all this data down to the phone and drop it in a database? Is there some issue with your app querying a database hosted on the webserver that holds the .txt? What sort of access to the .txt do you have? Can you modify it on the server so parsing can be done quicker?
In short, I think we need more data to analyze your issue. Can you post some examples of lines from the .txt and the code you use to parse it?
Upvotes: 2