Firebase DataSnapshot getValue does not set properties on init body

巧了我就是萌 提交于 2019-12-11 06:02:07

问题


I have a datasnopshot returned from my firebase database and set it up to parse the json to my model class.

override fun onDataChange(snapshot: DataSnapshot) {


             if(snapshot.exists()){

                 newReleases.clear()
                 for(d in snapshot.children){
                     val media = d.getValue(PlayableMedia::class.java)
                     newReleases.add(media!!)
                 }

                 newReleasesAdapter!!.notifyDataSetChanged()

             }

         }

Now this works if I am not doing anything during initialization. My problem is when I do something during initialization, my properties on the model class is empty or is set to their default value. I read from here about properties and all. This is my model class and its init body. I think I misunderstood the flow of the class initialization. Any help is appreciated.

class PlayableMedia(@set:PropertyName("id")
                @get:PropertyName("id")
                var id: String="",
                @set:PropertyName("audio_url")
                @get:PropertyName("audio_url")
                var audio_url: String="",
                @set:PropertyName("title")
                @get:PropertyName("title")
                var title: String = "",
                @set:PropertyName("artist")
                @get:PropertyName("artist")
                var artist: String = "") {

public var metadata: MediaMetadata? = null
public var hasOfflineCopy = false
var isDownloading = false

constructor():this("","","","")
init {
    MediaCacheWorkerTask(MyApplication.getAppContext(), object : MediaCacheCallback {
        override fun onSnapshotFound(stream: FileInputStream) {
            hasOfflineCopy = true
        }

        override fun onSnapshotMissing(url: String) {}
        override fun onSnapshotDownloaded(downloaded: Boolean) {}
    }).execute(audio_url) <--all properties are empty/default at this point

}

来源:https://stackoverflow.com/questions/54454616/firebase-datasnapshot-getvalue-does-not-set-properties-on-init-body

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