我尝试使用添加了Kotlin 1.1.4的新@Parcelize
注释以及包含RealmList属性的Realm对象。
@Parcelize
@RealmClass
open class Garage(
var name: String? = null,
var cars: RealmList<Car> = RealmList()
) : Parcelable, RealmModel
注释不支持RealmList
,并假设@Parcelize
专门用于避免创建方法,支持RealmList
的解决方案是什么?
提前致谢
答案 0 :(得分:4)
<强> 编辑: 强>
使用Type Parcelers和@WriteWith
注释的强大功能,可以创建一个可以为您处理此场景的RealmList类型的parceler:
使用以下代码:
fun <T> Parcel.readRealmList(clazz: Class<T>): RealmList<T>?
where T : RealmModel,
T : Parcelable = when {
readInt() > 0 -> RealmList<T>().also { list ->
repeat(readInt()) {
list.add(readParcelable(clazz.classLoader))
}
}
else -> null
}
fun <T> Parcel.writeRealmList(realmList: RealmList<T>?, clazz: Class<T>)
where T : RealmModel,
T : Parcelable {
writeInt(when {
realmList == null -> 0
else -> 1
})
if (realmList != null) {
writeInt(realmList.size)
for (t in realmList) {
writeParcelable(t, 0)
}
}
}
您可以定义如下界面:
interface RealmListParceler<T>: Parceler<RealmList<T>?> where T: RealmModel, T: Parcelable {
override fun create(parcel: Parcel): RealmList<T>? = parcel.readRealmList(clazz)
override fun RealmList<T>?.write(parcel: Parcel, flags: Int) {
parcel.writeRealmList(this, clazz)
}
val clazz : Class<T>
}
您需要为RealmList<Car>
创建一个特定的parceler,如下所示:
object CarRealmListParceler: RealmListParceler<Car> {
override val clazz: Class<Car>
get() = Car::class.java
}
但是,现在你可以做到以下几点:
@Parcelize
@RealmClass
open class Garage(
var name: String? = null,
var cars: @WriteWith<CarRealmListParceler> RealmList<Car> = RealmList()
) : Parcelable, RealmModel
和
@Parcelize
@RealmClass
open class Car(
..
): RealmModel, Parcelable {
...
}
这样您就不需要手动编写Parceler逻辑。
原始答案:
以下应该工作:
@Parcelize
open class Garage: RealmObject(), Parcelable {
var name: String? = null
var cars: RealmList<Car>? = null
companion object : Parceler<Garage> {
override fun Garage.write(parcel: Parcel, flags: Int) {
parcel.writeNullableString(name)
parcel.writeRealmList(cars)
}
override fun create(parcel: Parcel): Garage = Garage().apply {
name = parcel.readNullableString()
cars = parcel.readRealmList()
}
}
}
只要您还添加以下扩展功能:
inline fun <reified T> Parcel.writeRealmList(realmList: RealmList<T>?)
where T : RealmModel,
T : Parcelable {
writeInt(when {
realmList == null -> 0
else -> 1
})
if (realmList != null) {
writeInt(realmList.size)
for (t in realmList) {
writeParcelable(t, 0)
}
}
}
inline fun <reified T> Parcel.readRealmList(): RealmList<T>?
where T : RealmModel,
T : Parcelable = when {
readInt() > 0 -> RealmList<T>().also { list ->
repeat(readInt()) {
list.add(readParcelable(T::class.java.classLoader))
}
}
else -> null
}
以及
fun Parcel.writeNullableString(string: String?) {
writeInt(when {
string == null -> 0
else -> 1
})
if (string != null) {
writeString(string)
}
}
fun Parcel.readNullableString(): String? = when {
readInt() > 0 -> readString()
else -> null
}