I have an ArrayList of custom, simple Serializable objects I would like to cache to disk and read on re-launch. My data is very small, about 25 objects and at most 5 lists so I think SQLite would be overkill. In the iPhone world I would use NSKeyedArchiver and NSKeyedUnarchiver which works great. On Android I've attempted to do this with with a FileOutputStream and ObjectOutputStream and while the result is the same, the performance is terrible. Is there a better (read faster) way to cache small objects to the file system in Android?
Perhaps you'd get faster serialisation by implementing Android's Parcelable
interface, but it seems odd that you're getting such poor performance just from native serialisation.
I would say it's easiest to use a SharedPreferences
object to store data, but that doesn't really help for complex object types.
Anyway, I've worked on an app that uses SQLite extensively and the access times are ridiculously fast. For the time it takes to implement, you could give it a go.
For what it worth I cache some of my String data to disk using BufferedWriter/BufferedReader and it's very fast. Matter of fact it is faster than storing the same data to SharedPreferences. The code goes something like this (note that things happen faster when you provide buffer size)
final BufferedWriter out = new BufferedWriter(new FileWriter(file), 1024);
out.write(stuff);
out.close();
It's hard to know without profiling but my guess is your poor performance is down to using ObjectOutputStream
. Have you tried writing your own writeObject(ObjectOutputStream)
and readObject(ObjectOutputStream)
methods as this may help performance.
You could use the traceview
tool to see exactly where the application is running slow. Have a look at this question for instructions on how to use traceview
.