gpt4 book ai didi

java - 序列化与 Parcelable Android

转载 作者:太空狗 更新时间:2023-10-29 15:43:36 26 4
gpt4 key购买 nike

从事缓存项目。想用 Android 上的文件系统来做。我知道数据库也是一种选择,但想尝试文件系统。

在 Android 的情况下,使对象可序列化非常容易,使用 ObjectOutputStream 和 ObjectInputStream 来序列化和反序列化整个对象数组列表是自动发生的。

它还允许在两个 Activity 之间传递对象。但是,在读取 250 个节点的列表时,我的应用程序的加载时间增加了 5 倍。

我知道 parcelable 也执行传递部分,并且是 Android 的序列化方式。但是,任何人都知道如何将 parcelable 与 ObjectOutputStream 一起使用,以便可以充分利用 Android 的序列化。

此外,如果有人有想法如何加速 readObject 和 writeObject 的序列化,请分享。通过这样做可以实现多大的不同,这样做是否明智。

或者在 android 的情况下,数据库方式是缓存的唯一选择。不要忘记预缓存也可以通过 db 实现,但我们可以吗?

最佳答案

不要使用包裹。根据 Android 文档 (http://developer.android.com/reference/android/os/Parcel.html):

Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.

让一个对象实现可序列化接口(interface)也是有风险的,因为那样你就被绑定(bind)到那个序列化格式。如果您将来更改该对象,您将在读取在旧制度下序列化的对象时遇到问题。

使用数据库可能是将内容缓存到磁盘的最佳选择。

关于java - 序列化与 Parcelable Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7731748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com