有没有办法处理firebase阵列中的连续更新
我正在开发一个颤振应用程序,在这个程序中,我不断地收听一个BLE设备,当轮胎压力发生任何变化时,该设备会广播数据。这是一个轮胎压力监测系统 当用户开始新的骑乘时,我创建该骑乘的文档并开始收听BLE上的压力传感器。每当它广播新值时,我将其连接到有没有办法处理firebase阵列中的连续更新,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我正在开发一个颤振应用程序,在这个程序中,我不断地收听一个BLE设备,当轮胎压力发生任何变化时,该设备会广播数据。这是一个轮胎压力监测系统 当用户开始新的骑乘时,我创建该骑乘的文档并开始收听BLE上的压力传感器。每当它广播新值时,我将其连接到rideData数组中。 它工作得很好 当我想停下来的时候,问题就出现了,我会给同一个文档写一个结束时间,它就结束了。但是它需要一段时间,直到它发布所有以前的值来进行fire存储。这些值非常频繁,并且它一直在推送。所以结束一段旅程就像排队等候轮到它,过了一段
rideData
数组中。
它工作得很好
当我想停下来的时候,问题就出现了,我会给同一个文档写一个结束时间,它就结束了。但是它需要一段时间,直到它发布所有以前的值来进行fire存储。这些值非常频繁,并且它一直在推送。所以结束一段旅程就像排队等候轮到它,过了一段时间它就结束了旅程
它应该立即发生,
我试图创建一个状态,并检查只有当状态为false时才会推送骑乘数据。单击结束骑乘按钮时,我处于该状态,但需要时间
这里是我在firebase上推送数据的代码
void deviceData(firstDevice, secondDevice) async {
LocationData _locationData;
_locationData = await location.getLocation();
var firstDeviceData = parseManufacturerData(firstDevice);
var secondDeviceData = parseManufacturerData(secondDevice);
if (mounted) {
print("mounted is $mounted");
setState(() {
frontPressure = (firstDeviceData["pressure"]);
frontTemperature = (firstDeviceData["temperature"]);
rearPressure = (secondDeviceData["pressure"]);
rearTemperature = (secondDeviceData["temperature"]);
});
}
var newData = {
"lat": _locationData.latitude,
"long": _locationData.longitude,
"time": new DateTime.now(),
"front": firstDeviceData,
"rear": secondDeviceData
};
if (!isEnding) {
auth.currentUser().then((value) async {
if (value != null) {
if (value.uid != null) {
Firestore.instance
.collection("userCurrentRides")
.where('user', isEqualTo: value.uid)
.where('isActive', isEqualTo: true)
.snapshots()
.first
.then((event) {
event.documents.first.data;
if (event.documents.length > 0) {
print(newData);
String docId = event.documents.first.documentID;
Firestore.instance
.collection('userCurrentRides')
.document(docId)
.updateData(
{
"rideData": FieldValue.arrayUnion([newData])
},
).then((value) {
print('new update');
});
}
});
}
}
});
}
}
要停止骑乘,我使用以下功能
void stopRide(context) {
setState(() {
isEnding = true;
frontPressure = "0.0";
frontTemperature = "0.0";
rearPressure = "0.0";
rearTemperature = "0.0";
});
flutterBlue.stopScan();
if (currentSubscription != null) {
currentSubscription.cancel();
}
auth.currentUser().then((value) async {
if (value != null) {
Firestore.instance
.collection("userCurrentRides")
.where('user', isEqualTo: value.uid)
.where('isActive', isEqualTo: true)
.snapshots()
.first
.then((currentRideSnapshot) {
var local = currentRideSnapshot.documents.first.data;
local["endTime"] = new DateTime.now();
String docId = currentRideSnapshot.documents.first.documentID;
Firestore.instance
.collection('userCurrentRides')
.document(docId)
.setData({"endTime": new DateTime.now(), "isActive": false},
merge: true).then((value2) {
Firestore.instance
.collection("userCurrentRides")
.where('user', isEqualTo: value.uid)
.snapshots()
.first
.then((currentRideSnapshot) {
_progressHUD.state.dismiss();
Navigator.pop(context, true);
});
});
});
}
});
}
deviceData()
方法多久执行一次?它不是非常适合频繁使用(每秒几次),这取决于设备的广播频率,有时可能是每秒10次