可序列化对象放在捆绑包中时是否总是被序列化?
我们想知道当将 Bundle
与可序列化或可打包对象一起使用时,编组实际上何时发生?是不是一放到包里就可以了?由于包主要用于在两个屏幕之间简单地传递数据(我们在这里甚至不讨论 IPC!),编组对象似乎没有多大意义,因为它始终保留在内存中,不?
我们是否正确地假设,仅当
- 数据必须传递到另一个进程(例如在 RMI 期间)或
- 组件(活动或服务)被破坏且实例状态必须写入磁盘时,才会发生 编组(无论是 Java 序列化还是 Android 打包) ?
我见过 Android 框架工程师(我相信是 Dianne Hackborn)说应该使用 Parcelable
而不是 Serialized
,因为前者要快得多。快多少?如果对象在大多数情况下都没有被整理(假设我们对此的假设是正确的),这会产生影响吗?
We were wondering if when using Bundle
with serializable or parcelable objects, when does the marshalling actually happen? As soon as you put it in the bundle? Since bundles are mostly used to simply pass around data between two screens (we're not even talking about IPC here!), there doesn't seem to be much point in marshalling an object, since it stays in memory all the time, no?
Are we right in assuming that marshalling (be it Java serializing or Android parcelling) only happens if
- the data must be passed to another process, e.g. during RMI, or
- the component (activity or service) gets destroyed and instance state must be written to disk?
I've seen Android framework engineers (I believe it was Dianne Hackborn) say that one should use Parcelable
instead of Serializable
because the former is much faster. How much faster? And will this even make a difference if the object isn't marshalled most of the time anyway (assuming our assumptions about this were right)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想我已经明白了。我基本上花了最后一天和今天的大部分时间来调试 Android
Parcel
和Bundle
源代码,它的工作原理如下:HashMap
,但它支持打包(即编组)内部映射及其内容所以一般来说:不,一个值不能简单地通过将其放入 Bundle 中来进行打包。相反,当将 Bundle 传递给另一个组件(活动或服务;为什么 Android 会这样做,我不知道,因为技术上没有 IPC 发生)或者当它必须被打包时,会发生打包。
I think I've figured it out. I basically spent the entire last day and most of today on debugging through the Android
Parcel
andBundle
source code, and here is how it works:HashMap
, but it supports to parcel (i.e. marshal) that internal map and its contentsSo generally: NO, a value is not parceled simply by putting it into a Bundle. Instead, parcelling happens when passing the Bundle to another component (activity or service; why Android does that, I don't know, since no IPC is happening technically.) or when it otherwise has to be parcelled.
我认为这会立即发生。我认为性能的提高是因为可序列化需要反射才能工作。我认为这与可序列化和可外部化之间的性能差异相同。
I think it happens right away. And I think the performance increase is because of serializable needed reflection to work. I think it's the same as the performance difference between serializable and externalizable.