Firebase DataSnapshot getValue does not set proper

2019-08-10 05:25发布

问题:

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

}