-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #859 from Hi-Windom/v0.35
V0.35
- Loading branch information
Showing
12 changed files
with
40 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
title: 序列化 | ||
--- | ||
## kotlin compose中使用@Serializable好还是@Parcelize好 | ||
|
||
在Kotlin的Compose框架中,数据序列化是一个重要的方面,尤其是在处理UI状态、持久化或跨组件传递数据时。`@Serializable`和 `@Parcelize`是两种常用的注解,用于实现数据序列化,但它们的使用场景和优势各不相同。 | ||
`@Serializable`是Kotlin标准库的一部分,它允许你轻松地将数据类对象转换为JSON格式,反之亦然。这对于网络请求、本地存储和与其他系统交换数据非常有用。`@Serializable`注解通过Kotlin的序列化库支持,可以处理复杂的数据结构和嵌套类,同时提供了灵活的定制选项,比如自定义序列化器和忽略某些属性。 | ||
另一方面,`@Parcelize`是Android KTX库的一部分,专为Android开发设计,用于实现Parcelable接口。Parcelable是一种高效的序列化机制,专门用于Android中的跨进程通信(IPC)。它比Serializable更快,因为它直接将数据序列化为字节流,适合在Android组件之间传递数据,如Intent和Bundle。 | ||
在决定使用哪个注解时,应考虑以下几点: | ||
|
||
1. **性能需求**:如果你需要高效的跨进程通信,尤其是在Android应用中,`@Parcelize`可能是更好的选择。 | ||
2. **数据交换格式**:如果你需要将数据序列化为JSON或其他格式,或者需要与不支持Parcelable的系统交换数据,`@Serializable`更适合。 | ||
3. **平台兼容性**:`@Serializable`可以在任何支持Kotlin的平台中使用,而 `@Parcelize`仅限于Android。 | ||
4. **复杂性和灵活性**:如果你的数据结构复杂,需要更多的定制和灵活性,`@Serializable`提供了更多的选项。 | ||
在实际应用中,你可能需要根据具体情况选择最合适的注解。例如,对于Compose中的状态管理,如果状态对象需要在Compose组件之间传递,并且需要跨进程传递,那么 `@Parcelize`可能是更好的选择。如果状态对象需要被持久化到本地存储,或者通过网络传输,那么 `@Serializable`可能更合适。 | ||
总之,没有绝对的“好”与“坏”,应根据具体需求和应用场景来选择最合适的序列化方式。 | ||
|
||
|
||
由于 `@Parcelize`依赖于Android的Parcelable机制,它不适用于Kotlin的多平台项目,这些项目可能会针对除了Android之外的其他平台,如JVM、iOS、Linux等。在Kotlin的多平台项目中使用Compose时,如果您需要序列化数据,应该使用 `@Serializable`注解。`@Serializable`是Kotlin的跨平台序列化机制的一部分,它允许您在不同的平台之间轻松地序列化和反序列化数据。这意味着您可以在多平台项目中使用 `@Serializable`来序列化Compose中的状态对象,而不用担心平台兼容性问题。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "Sillot-workspace", | ||
"version": "0.35.24070622", | ||
"version": "0.35.24070801", | ||
"description": "汐洛 pnpm 工作区", | ||
"packageManager": "[email protected]", | ||
"scripts": { | ||
|