When my application starts I need to load about 500 objects from an xml page, like this:
<object>
<name>objectname</name>
<info>info</info>
<info2>info</info2>
<info3>info</info3>
<info4>info</info4>
<alias>false</alias>
</object>
Now I want to store this on the device, hoping the reading will be faster. Currently I use an ObjectOutputStream to write the objects.
private static void write(ArrayList<MyObject> objects, String fileName, Context context) {
final File cacheDir = context.getCacheDir();
final File objectsFile = new File(cacheDir.getAbsoluteFile() + File.separator + fileName);
FileOutputStream fos = null;
ObjectOutputStream oos = null;
boolean keep = true;
try {
fos = new FileOutputStream(objectsFile);
oos = new ObjectOutputStream(fos);
oos.writeObject(objects);
} catch (Exception e) {
e.printStackTrace();
keep = false;
} finally {
try {
if (oos != null)
oos.close();
if (fos != null)
fos.close();
if (keep == false)
objectsFile.delete();
} catch (Exception e) {
}
}
}
This is not a very fast solution, reading can take about 10-15 seconds. I’m showing the objects in a listview, so all objects need to be read in at once.
Any ideas?
I think the best method to store such data would be in a database (see here).
Parse once and store the information in the database (one column for each attribute). It should be pretty fast to retrieve 500 records from the database 🙂