I work on android chat app and it work fine until i added this line to myapplication classFirebaseDatabase.getInstance().setPersistenceEnabled(true);
to enable getting offline users messages it cause my app to crash with this error message
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to long
and if i removed this line from myApplication class app work fine , i know it is because i get data in pojo class ,how to do that to get benefit of firebase offline capabilities and pass these data to recyclerview adapter my fetch message method
DatabasReference ref = FirebaseDatabase.getInstance().getreference().child("messages") private void fetchMessages() { if (mChildEventListener == null) { mChildEventListener = new ChildEventListener() { @Override public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { if (snapshot.exists()) { Messages messages = snapshot.getValue(Messages.class); messagesList.add(messages); adapter.notifyDataSetChanged(); mRecyclerView.smoothScrollToPosition(messagesList.size() - 1); } } @Override public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { } ref.addChildEventLustener(mChildEventListener)
My database structure
"Users": { "useId": { "name": m, "image": true } "useId": { "name": n, "image": true } } "Messages" :{ "mesageId" : { messsage:"hello" from: "MERjk5566699jjg" photoUrl : null date:145885665255 seen :false
and message class
public class Messages { private String message,from ,photoUrl; private long date; private boolean seen; public Messages() { } public Messages(String message, String from, String photoUrl, long date, boolean seen) { this.message = message; this.from = from; this.photoUrl = photoUrl; this.date = date; this.seen = seen; }
Advertisement
Answer
I answer my question to help anyone if he get an error like
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to long
when using FirebaseDatabase.getInstance().setPersistenceEnabled(true);
the error occurred because I retrieve data like
Messages messages = snapshot.getValue(Messages.class);
it is not right to do that when using firebase offline ,the right way to retrieve every child like
String text = snapshot.child("message").getValue().toString;
and then create instance of your class and pass it to adapter