-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| plugins { | ||
| id("com.android.application") | ||
| id("kotlin-android") | ||
| id("kotlin-parcelize") | ||
| } | ||
|
|
||
| android { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,24 +8,25 @@ import java.util.concurrent.CopyOnWriteArrayList | |
| class ParcelDiffHelper<T : Parcelable> : BaseDiffHelper<T>() { | ||
|
|
||
| override fun clone() { | ||
| try { | ||
| itemsCursor?.apply { | ||
| snapshot = CopyOnWriteArrayList() | ||
| for (entity in this) { | ||
| val parcel = Parcel.obtain() | ||
| (entity as Parcelable).writeToParcel(parcel, 0) | ||
| parcel.setDataPosition(0) | ||
| val constructor = entity.javaClass.getDeclaredConstructor(Parcel::class.java) | ||
| constructor.isAccessible = true | ||
| val dateEntity = constructor.newInstance(parcel) as T | ||
| snapshot?.add(dateEntity) | ||
| parcel.recycle() | ||
| } | ||
| itemsCursor?.run { | ||
| snapshot = CopyOnWriteArrayList() | ||
| forEach { | ||
| snapshot?.add(it.deepCopy() ?: it) | ||
| } | ||
| } catch (e: Exception) { | ||
| e.printStackTrace() | ||
| } | ||
| } | ||
|
|
||
| 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() | ||
| } | ||
| } | ||
|
Comment on lines
+19
to
+30
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这种深拷贝实现更具有普适性吧,
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 估计当时写的时候是没考虑kotlin-parcelize 我觉得可以啊 这么改
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我昨晚照抄代码发现的抛异常问题来着
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 其实我后面想用霍老的深拷贝框架的 但是太懒了 就没动手
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 其实一般场景下用 data class 的 copy 就好了,完全深拷贝不是很有必要 |
||
| } | ||
|
|
||
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.
恩 问题不大 我觉得