Skip to content

Conversation

@Goooler
Copy link
Contributor

@Goooler Goooler commented Apr 25, 2022

No description provided.

Comment on lines +19 to +30
private fun <T : Parcelable> T.deepCopy(): T? {
var parcel: Parcel? = null
return try {
parcel = Parcel.obtain().also {
it.writeParcelable(this, 0)
it.setDataPosition(0)
}
parcel.readParcelable(this::class.java.classLoader)
} finally {
parcel?.recycle()
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种深拷贝实现更具有普适性吧,entity.javaClass.getDeclaredConstructor(Parcel::class.java) 那里会在用了 kotlin-parcelize 插件之后抛异常的,因为没有那个构造器

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

估计当时写的时候是没考虑kotlin-parcelize 我觉得可以啊 这么改

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我昨晚照抄代码发现的抛异常问题来着

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实我后面想用霍老的深拷贝框架的 但是太懒了 就没动手

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实一般场景下用 data class 的 copy 就好了,完全深拷贝不是很有必要

}
itemsCursor?.run {
snapshot = CopyOnWriteArrayList()
forEach {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用了 deepCopy 的扩展之后没必要异常处理了

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前是担心出现沙雕异常导致的

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那就先去掉喽?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

恩 问题不大 我觉得

@Leifzhang Leifzhang merged commit 5f864c4 into Leifzhang:master Apr 25, 2022
@Goooler Goooler deleted the diff branch April 25, 2022 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants