Firebase with Realm. De-serializing POJO class

前端 未结 2 1221
难免孤独
难免孤独 2020-12-12 04:36

I have data in my firebase DB, everything works fine until I try to De-serialize the data.

Error: argument 1 has type io.realm.RealmList, got java.util.Array

2条回答
  •  Happy的楠姐
    2020-12-12 04:48

    Firebase won't work with classes that do not have default constructor or private variables i.e no public getter/setter.

    A easier solution in your case would be to make a middleware class that is the same pojo just not extending RealmObject. Next initialise your RealmObject subclass using the values of the pojo.

    Pseudo code

    class SimplePojoPlaylist  {
         public String variable;
    }
    
    class Playlist extends RealmObject {
         public String variable;
    }
    

    Then first cast into SimplePojoPlaylist

        for (DataSnapshot child : dataSnapshot.getChildren()) {
             SimplePojoPlaylist receivedPlaylist = child.getValue(SimplePojoPlaylist.class);
             Playlist playList = new Playlist();
             playList.variable = receivedPlaylist.variable;
        }
    

提交回复
热议问题