-
Notifications
You must be signed in to change notification settings - Fork 7
Optimize ParcelDiffHelper #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| 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() | ||
| } | ||
| } |
There was a problem hiding this comment.
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 插件之后抛异常的,因为没有那个构造器
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
估计当时写的时候是没考虑kotlin-parcelize 我觉得可以啊 这么改
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我昨晚照抄代码发现的抛异常问题来着
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其实我后面想用霍老的深拷贝框架的 但是太懒了 就没动手
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用了 deepCopy 的扩展之后没必要异常处理了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
之前是担心出现沙雕异常导致的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那就先去掉喽?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
恩 问题不大 我觉得
No description provided.