【问题标题】:Firebase data gets overwrittenFirebase 数据被覆盖
【发布时间】:2016-05-03 15:10:59
【问题描述】:

每次我的位置更改时,我都希望它向相同的根 ID 添加一个航点,但每次它只是覆盖根内的航点。 已将 .setValue 更改为 .updateChildren 但仍然没有运气。

Firebase mainRef = null;
private void saveToFirebase() {

    while (isFirstTime) {
        // Generates root id
        mainRef = myFirebaseRef.push();
        isFirstTime = false;
    }


    // Generates new id for waypoint
    Firebase wayRef1 = mainRef.push();
    String waypointKey = wayRef1.getKey();


    // Create waypoint post
    Map<String, Object> newPost = new HashMap<String, Object>();
    newPost.put("latitude", mLastLocation.getLatitude());
    newPost.put("longitude", mLastLocation.getLongitude());


    // POST HOLDER
    Map<String, Object> waypointHolder = new HashMap<>();
    waypointHolder.put(waypointKey, newPost);


    // PARENT OF WAYPOINTHOLDER
    Map mParent = new HashMap();
    mParent.put("timeStamp", mLastUpdateTime);
    mParent.put("waypoints", waypointHolder);
    mainRef.updateChildren(mParent);

}

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    看起来您正在创建航点值的哈希图,但在添加新值之前您没有将现有值加载到哈希图中。因此,您每次都在创建一个新的 hashmap,向其中添加一个条目,然后将其发布到 firebase。这会产生一个 hashmap,每次都将一个航路点条目存储在 firebase 中。

    【讨论】:

    • 糟糕,感谢您的帮助!
    猜你喜欢
    • 2015-09-26
    • 2019-04-28
    • 1970-01-01
    • 2020-03-15
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多