kotlin - realm and parcel

蓝咒 提交于 2019-12-07 14:13:31

问题


I develop the android project with android studio. I want to save realm objects using onSaveInstanceState() in kotlin. My code is

@Parcel( implementations = arrayOf(UserRealmProxy::class),
         value = Parcel.Serialization.BEAN,
         analyze = arrayOf(User::class))
open class User : RealmObject() {
    open var name: String? = null

    @ParcelPropertyConverter(ListParcelConverter::class)
    open var Items: RealmList<Item>? = null
}

but there are some errors while compiling:

'Unresolved reference: UserRealmProxy'
'An annotation parameter must be a compile-time constant'

Sure, UserRealmProxy exists already because already the project has been compiled. also @ParcelPropertyConverter(ListParcelConverter::class) does not work. it causes exception in runtime:

'java.io.NotSerializableException: io.realm.RealmList'

But this code is compiled well in java.

i need your help.


回答1:


Probably, you will annotate to setter. Please try this.

@Parcel( implementations = arrayOf(UserRealmProxy::class),
     value = Parcel.Serialization.BEAN,
     analyze = arrayOf(User::class))
open class User : RealmObject() {
    open var name: String? = null
    open var Items: RealmList<Item>? = null
    @ParcelPropertyConverter(ListParcelConverter::class) set
}



回答2:


In my case, when I have this error: 'Unresolved reference: UserRealmProxy'. If I have this package: com.path.model.Album, and I have to use:

import io.realm.com_path_model_AlbumRealmProxy

...
@Parcel(implementations = arrayOf(com_path_model_AlbumRealmProxy::class)


来源:https://stackoverflow.com/questions/38910246/kotlin-realm-and-parcel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!