簡體   English   中英

如何自動將 Firebase 實時數據庫寫入 Firestore?

[英]How to write Firebase Realtime Database to Firestore automatically?

我目前正在做一個項目,使用 Arduino 收集數據並將數據存儲在雲中,並在移動應用程序上顯示雲上的數據。 根據我的嘗試,Arduino 只能將數據上傳到 Firebase 實時數據庫,但我的 Flutter 移動應用程序需要將數據存儲在 Firestore 中。 有沒有辦法在實時數據庫發生變化時自動將firebase實時數據庫數據寫入Firestore?

這是實時數據庫結構:實時數據庫結構

我想將 append 數據放入 Firestore 中的相應字段,如下所示: firestore structure

我嘗試每 5 秒讀取一次數據並將其寫入 Firebase,但它似乎效率不高:

final userDbRef = FirebaseDatabase.instance.ref("user/");
 String uid = globals.uid;

 Stream<DatabaseEvent> stream = userDbRef.onValue;

 // Subscribe to the stream!
 var subscription = stream.listen((DatabaseEvent event) {
   // print('Listening');
   dynamic values = event.snapshot.value;
   final data = new Map<String, dynamic>.from(values);

   globals.temp = data['temp'];
   globals.hr = data ['hr'];
   globals.spo2 = data['spo2'];
   globals.position = data['position'];
   globals.time = DateTime.parse(data['time']);
 });

 subscription.pause();

 String date = DateTime.now().add(const Duration(hours: 8)).toString().substring(0, 10);

 dynamic result = await DatabaseService(uid: uid).getUserData('patient');
 if (result[date].toString() == 'null') {
   await DatabaseService(uid: uid).updateSingleUserData('patient', {
     date: {
       'time': ['${date} 00:00:00.000'],
       'hr': [95],
       'temp': [36.5],
       'spo2': [98],
       'position': ['lying'],
     },
   });
 } else {
   List timeList = result[date]['time'];
   List hrList = result[date]['hr'];
   List tempList = result[date]['temp'];
   List spo2List = result[date]['spo2'];
   List positionList = result[date]['position'];

   if ((!timeList.contains(globals.time.toString())) & (globals.time.toString() != 'null')) {
     timeList.add(globals.time.toString());
     hrList.add(globals.hr);
     tempList.add(globals.temp);
     spo2List.add(globals.spo2);
     positionList.add(globals.position);

    await DatabaseService(uid:uid).updateSingleUserData('patient', {
       date: {
         'time': timeList,
         'hr': hrList,
         'temp': tempList,
         'spo2': spo2List,
         'position': positionList,
       },
     });
   }
 }
 subscription.resume();

謝謝你。

有沒有辦法在實時數據庫發生變化時自動將firebase實時數據庫數據寫入Firestore?

是的,一種常見的方法是使用 Cloud Function ,每次將新節點添加到實時數據庫時都會自動觸發。 Cloud Function 在后端執行,即在 Firebase/Google Cloud 基礎設施上,因此您無需在 Flutter 應用程序中為此數據鏡像/傳輸編寫任何代碼。

這是一個帶有通用路徑的簡單示例:

// The Cloud Functions for Firebase SDK to create Cloud Functions and set up triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access Firestore.
const admin = require('firebase-admin');
admin.initializeApp();


exports.duplicateToFirestore = functions.database.ref('/myRTDBPath/{pushId}')
    .onCreate((snapshot, context) => {
      // Grab the current value of what was written to the Realtime Database.
      const nodeVal = snapshot.val();

      // Write it to a Firestore collection

      return admin.firestore().collection('myFirestoreCollection').add(nodeVal);

    });

我讓您完成將 RTDB 節點值轉換為您正在尋找的 Firestore 文檔結構的工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM