dmdevgo / Premo

Premo helps to implement the presentation layer and share it on Android, iOS, Desktop and Web.
MIT License
199 stars 12 forks source link

Parcelable insted of JSON #6

Closed drdla49 closed 2 years ago

drdla49 commented 2 years ago

We use your library version 1.0.0 alpha 04 in android.

You use JSON to saveState.

We can use parcelable instead of JSON.

It's possible to support parcelable for saving state? We have a bad experience with the speed of encode and decode JSON.

Thanks.

dmdevgo commented 2 years ago

Hi @drdla49, thanks for your feedback. Premo is a multiplatform library. I don't want to be limited to using a platform-specific way of serialisation (such as Parcelable). Therefore, I suggest using Kotlinx.serialization. Note that Kotlinx.serialization supports not only JSON, but also other formats, such as ProtoBuf.

First of all, I would like to understand the reasons for the bad experience and whether serialization is really a bottleneck in your case: 1) What data do you store in the bundle, does the state contain any collections? How big is the backstack of presentation models. How deep is the tree of children? 2) Please, to benchmark a speed of serialization/deserialization. It is best to do this in the StateSaver. 3) You can try using binary format like ProtoBuf instead of JSON. Ideally, write a test to compare these approaches. 4) Is serialization really slowing down or slowing down due to the creation of a root presentation model with a backstack and children, which in turn can request their dependencies.

If you still need Parcelable, take a look at these repositories: https://github.com/chRyNaN/serialization-parcelable https://github.com/AhmedMourad0/bundlizer

drdla49 commented 2 years ago

We have a big app with bottom bar navigation, so we have 4 stacks with many backstacks. We are at the beginning of using your library, and we now looking for the best solution for multiplatform navigation in our app. But when we tried to find multiplatform solution for savestate/send data in intents etc. And we compare chRyNaN serialization and JSON serialization and JSON is slower.

We can use any multiplatform solution to save state that you implement in your library. But now your PmStateSaver has only methods with String as a parameter.

fun <T> saveState(kType: KType, value: T): String fun <T> restoreState(kType: KType, jsonString: String): T

lepicekmichal commented 2 years ago

Hi, I got the same problem. A string is too slow compared to parcelable. There is no need to break multiplatform, just make the functions through expect/actual where the android platform will use parcelable/string while other platforms only string

Would be a big deal for us :) Thx

dmdevgo commented 2 years ago

Good news. I refactored the PmStateSaver so that any serialization can be implemented.

When creating the PmActivityDelegate, you need to pass a factory to create a saver for each presentation model:

interface PmStateSaverFactory {
     fun createPmStateSaver(key: String): PmStateSaver
}

The saver interface looks like this:

interface PmStateSaver {
     fun <T> saveState(key: String, kType: KType, value: T?)
     fun <T> restoreState(key: String, kType: KType): T?
}

@drdla49 @lepicekmichal review #7 and give your feedback, please.

lepicekmichal commented 2 years ago

That does seem to be what we wanted :)

dmdevgo commented 2 years ago

Support for different formats implemented in version 1.0.0-alpha.05. I close this issue.