带有updateChildren()的Firebase ServerValue#TIMESTAMP

时间:2019-03-10 12:43:14

标签: android firebase firebase-realtime-database timestamp

我有一个执行以下操作的映射器:

.float-wrap {
  display: flex;
  flex-wrap: nowrap;
}

.left-floater {
  flex: 0 0 100px;
}

.outer {
  overflow-x: auto
}

当我需要同时将更多模型发布到不同的位置时,我将使用映射器并将其隐藏到override fun map(model: Model) = mapOf<String, Any?>( ..., KEY_TIMESTAMP to (model.timestamp.takeIf { it > -1 } ?: ServerValue.TIMESTAMP), ... ) 模型中。 所以我有一堆地图,然后将它们合并到一个大的Map<String, Any?>中,看起来像这样:

Map<String, Any?>

所以我就这样做:

{
   "messages/-L_bXQUibioB6OMkl0SE/text": "Android Developer created this conversation",
   "messages/-L_bXQUibioB6OMkl0SE/senderName": "system",
   "messages/-L_bXQUibioB6OMkl0SE/senderAvatarUrl": null,
   "messages/-L_bXQUibioB6OMkl0SE/timestamp/.sv": "timestamp",
   "messages/-L_bXQUibioB6OMkl0SE/attachment": null,
   "chats/participants/UZ684hcHTnOkaglUX0QwbR9DK442/name": "cool guy",
   "chats/participants/UZ684hcHTnOkaglUX0QwbR9DK442/imageUrl": null,
   "chats/participants/aqiTRMAUOSdJIuEOvNJpeAb4D0F3/name": "Android Developer",
   "chats/participants/aqiTRMAUOSdJIuEOvNJpeAb4D0F3/imageUrl": "https://firebasestorage...",
   "chats/lastMessage": "Android Developer created this conversation",
   "chats/lastMessageTimestamp/.sv": "timestamp",
   "chats/name": null,
   "chats/imageUrl": null,
   "chats/type": "SINGLE"
}

调用最后一个代码时,出现以下异常:

ovveride fun updateChildren(data: Map<String, Any?>) = Completable.create { emitter ->
    rootRef.updateChildren(data) { databaseError, _ ->
        databaseError?.toException()
            ?.let(emitter::onError)
            ?: emitter.onComplete()
    }
}

如何解决此问题,并在可能的情况下仍使用ServerValue.TIMESTAMP?

1 个答案:

答案 0 :(得分:1)

因为这行:

"messages/-L_bXQUibioB6OMkl0SE/timestamp/.sv": "timestamp"

应如下所示:

"messages/-L_bXQUibioB6OMkl0SE/timestamp": {
    ".sv": "timestamp"
}

该错误位于我的地图合并方法中 最简单的解决方法是过滤此类属性:

lateinit var entry: MutableMap.MutableEntry<String, Any?>
while (map.entries.find { it.key.endsWith(".sv") }?.apply { entry = this } != null) {
    map.remove(entry.key)
    map[entry.key.removeSuffix("/.sv")] = ServerValue.TIMESTAMP
}

return realtimeDatabase.updateChildren(map)