我在 Java 中使用 Firebase。当必须更新用户信息时,以下简单代码始终可以正常工作:
final FirebaseBean_User userObject = new FirebaseBean_User(uuid, name, timestamp, points, gamesPlayed, gamesWon);
mFirebaseUser.setValue(userObject, System.currentTimeMillis());
我使用当前时间戳作为优先级值,以便我可以获得最近在线的所有用户的列表。
但是,当用户离线时,我想将他们标记为离线。因此,我在中间再次添加了一个简单的行:
final FirebaseBean_User userObject = new FirebaseBean_User(uuid, name, timestamp, points, gamesPlayed, gamesWon);
mFirebaseUser.onDisconnect().setValue(userObject, USER_PRIORITY_OFFLINE); // best practice: always call onDisconnect() before the actual setValue() operation to prevent ghost data
mFirebaseUser.setValue(userObject, System.currentTimeMillis());
突然,这段代码停止了工作。我收到以下异常,其中导致行 (465) 是中间的新行:
FATAL EXCEPTION: main
com.firebase.client.FirebaseException: Failed to parse node with class class com.my.package.MultiplayerService$FirebaseBean_User
at com.firebase.client.snapshot.NodeUtilities.NodeFromJSON(NodeUtilities.java:130)
at com.firebase.client.OnDisconnect.setValue(OnDisconnect.java:81)
at com.my.package.MultiplayerService.updateUserInformation(MultiplayerService.java:465)
at com.my.package.MultiplayerService.access$4(MultiplayerService.java:461)
at com.my.package.MultiplayerService$1.onDataChange(MultiplayerService.java:83)
at com.firebase.client.core.ValueListenerContainer$1.run(ValueListenerContainer.java:50)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
为什么会这样?我做错什么了吗?我的意思是,setValue()
以前工作得很好,现在唯一的变化是我也调用它onDisconnect()
并且它停止工作。
(在该参考点之前是否有数据并不重要。)
这是用于参考的类:
private static class FirebaseBean_User {
private String uuid;
private String name;
private int lastlogin;
private double points;
private int gamesplayed;
private int gameswon;
private FirebaseBean_User() { }
public FirebaseBean_User(String uuid, String name, int lastlogin, double points, int gamesplayed, int gameswon) {
this.uuid = uuid;
this.name = name;
this.lastlogin = lastlogin;
this.points = points;
this.gamesplayed = gamesplayed;
this.gameswon = gameswon;
}
public String getUuid() {
return uuid;
}
public String getName() {
return name;
}
public int getLastlogin() {
return lastlogin;
}
public double getPoints() {
return points;
}
public int getGamesplayed() {
return gamesplayed;
}
public int getGameswon() {
return gameswon;
}
@Override
public boolean equals(Object o) {
return (o instanceof FirebaseBean_User && ((FirebaseBean_User) o).getUuid() == this.uuid);
}
}