我在Firebase中使用ServerValue.TIMESTAMP进行查询排序。我将模型类中的时间戳设置为dats,例如1494325636954。
public Attendance(String usico) {
this.usico = usico;
HashMap<String, Object> datsObj = new HashMap<String, Object>();
datsObj.put("date", ServerValue.TIMESTAMP);
this.dats = datsObj;
}
如何设置datt负值ServerValue.TIMESTAMP,例如-1494325636954?
答案 0 :(得分:1)
您可以创建Firebase功能,将值更新为负值,同时保持服务器的准确时间戳。
exports.makeNegativeTimestamp = functions.database.ref('/posts/{postID}/date').onWrite(event => {
// get the timestamp from the DeltaSnapshot
const timestamp = event.data.val();
// ensure that the timestamp is a number
if (isNaN(timestamp)) { return; }
// only make negative if it's currently positive
if (timestamp >= 0) {
// returns a promise
return event.data.adminRef.set(timestamp * -1);
}
});
答案 1 :(得分:0)
在其他情况下使用timeStamp但是当涉及到firebase时,你不需要全部将timeStamp转换为负数来进行排序,我的方式是在每次添加子项时将long或long递减(在子项内部)然后使用它作为排序方法,如果你只关心订单,这将简化很多时间。