【问题标题】:FirebaseException in ref.updateChildren(): Path X is an ancestor of X/Y in an updateref.updateChildren() 中的 FirebaseException:路径 X 是更新中 X/Y 的祖先
【发布时间】:2015-10-09 13:42:40
【问题描述】:

在同一 ref.updatChildren() 操作中尝试使用“users/user”和“users/user/name”等路径执行多路径写入时,应用程序崩溃并出现以下异常:

com.firebase.client.FirebaseException:路径 X 是 X/Y 的祖先 更新。

在 Firebase 中执行这种多路径写入的最佳方式是什么?谢谢!

编辑: 在这种特殊情况下,我想实现以下目标:

HashMap<String, Object> userUpdate = new HashMap<String, Object>();
HashMap<String, String> user = new HashMap<String, String>();
user.put("firstName", "Ivan");
user.put("initialOfLastName", "V");

userUpdate.put("/user", user);
userUpdate.put("/user/gender", "male")

ref.updateChildren(userUpdate);

【问题讨论】:

    标签: java android firebase


    【解决方案1】:

    与错误状态一样,您不能为/user 指定更新,然后为/user/gender 指定不同的更新。将性别更新移至user 地图应该可以。

    user.put("firstName", "Ivan");
    user.put("initialOfLastName", "V");
    user.put("gender", "male")
    
    userUpdate.put("/user", user);
    
    ref.updateChildren(userUpdate);
    

    【讨论】:

    • 感谢您的回答,如果字段是字符串,这将有效。对于复杂/嵌套数据,解决方案是将“性别”的map作为写入路径
    • 如果字段不是字符串,则使用HashMap&lt;String, Object&gt; 而不是HashMap&lt;String, String&gt;
    【解决方案2】:

    对于这种情况,您必须在要更新的位置的顶层使用嵌套的 HashMap

    HashMap<String,String> userUpdate = new HashMap<String,String>();
    userUpdate.put("firstName", "Ivan");
    userUpdate.put("initialOfLastName", "V");
    ref.child(uid).updateChildren(userUpdate);
    

    【讨论】:

    • 通过该编辑,Anid 的答案是正确的。我也会撤回我的近距离投票。 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 2012-06-22
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 2013-04-22
    相关资源
    最近更新 更多