0

I was planning (and implemented -_- my bad because didn't read anything on this article) to use Java Serialization for creating data files from my objects, but as i noticed right now, it's fairly SLOW on android even with powerful devices.

Simply, i have an application that stores dozens of Stroke objects, i need to save and load those objects for later use. My current workflow looks like that ;

public class StrokePoint extends PointF implements Serializable { ... }
public class CardinalStroke extends Stroke implements Serializable {
    ...
    protected ArrayList<StrokePoint> mPoints = new ArrayList<StrokePoint>();
    ...
}

Finally, i have a class which is containing stroke objects, and the other things, it looks like ;

public class NoteElement implements Serializable {
    ...
    private ArrayList<IStroke> mStrokes = new ArrayList<IStroke>();
    ...
}

I'm using for serialization that code and saving serialized data into SQLITE DB (should i keep using SQLITE for this use or is it better to save those data into FILE for future using.);

private void saveElement(NoteElement element) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(element);
        mProvider.insertOrUpdateElement(mCurrentModel.getBookId(), element.getUuid(), baos.toByteArray());
        element.setIsModified(false);
    } catch (IOException e) {
    }
}

When i run saveElement serialization keeps about 500+ms at least with non-complex elements, i could have very complex NoteElements, they will keep probably 2000ms+ which is not acceptable for serializing that data, also saving into SQLITE keeps like Serialization/4 ms.

Well regarding my question is, How can I improve serialization speed, or should i use completely different technique for that kind of data storage. I read about Percelable but that couldn't be used for data storage maybe there is some alternative built-in or should i implement my own data serializer classes which could be pain in some point ? I'm open for any kind of suggestions, Thanks for your time!

This Question is not about SQLITE Slow insertation, it's about slow Serialization. I wonder who is marked that question is marked as duplicate to Slow SQLITE inseration weird.

Nande kore
  • 784
  • 1
  • 8
  • 16
  • its not that slow, but you do need to make it an async task, two seconds to load data back is ok, as long as you display some kind of loading widget and use services to fetch that data. Or maybe look at otto library for a message buss – abdu Sep 17 '14 at 14:28
  • @abdu thanks for your response, but 2+s saving and load back that data in my application looks problematic, ofcourse I'm gonna use AsyncTask for that kind of stuff but in my application user can navigate between those elements so quickly, everytime showing BusyIndicator on the application is so lame imho. Well if i stuck with that Serialization approach probably i'm gonna save to each Stroke object into DB instead whole Stroke container (NoteElement) :'/ sounds bad to me. – Nande kore Sep 17 '14 at 14:43
  • you will only save or load data when the app is closing / opening, once you have the data you dont save anything until user tries to close the app then start a task that will save the data and it will take 2 seconds only, and when the app start you load data back once and that is it. If you want to share that data make a singleton class and have all of you activities/fragments reference that singleton, don't serialize/deserialize data between activities. – abdu Sep 18 '14 at 06:26
  • @abdu hey, 1 NoteElement keeps about 2s around, and my Model has 100+ NoteElements, i'm not able to save or load whole document at the same time. I need to do that saving loading as Lazy pattern, As you already know that I also need to concern about memory consumption. Anyway still i wonder if there any other solution instead Regular serialization/de-serialization ! – Nande kore Sep 18 '14 at 11:36
  • Post your model classes so I can see what type of complexity you have, did you try using a singleton and holding 100+ notelements in memory? I dont think its that much, if you have 10 000 elements in one array list it will take around 0.2 megabytes of ram... – abdu Sep 18 '14 at 11:50

0 Answers0