如何观察从 Firebase 数据库中删除模型?

How to observe the removal of a model from the Firebase Database?

我使用 Firebase 数据库,我有一个卡片列表,我需要监控一些卡片是否被删除。我尝试用 ref.observe (.childRemoved) 来做到这一点,但这不适合我的情况,因为持卡人可以删除一些数据,然后这个块会触发几次,我需要观察是否整张卡都被删除了.我该怎么做?

我的参考

    let ref = FIRDatabase.database().reference().child(MainGateways.cards.rawValue).child(card.id) 

我的卡片结构

"cards": {
   "-Khv9rUVwErNHBzXcruO": {
      "additionalInfo": {
          "note": ""
      },
      "dateProperties": {
          "day": "Flex",
          "isFlexDate": true,
          "isFlexTime": true,
          "iso8601": "",
          "time": ""
      },
      "interestedUsers": {
          "NfM26A2YcPUFz8rfYa23Kr3mjCO2": {
              "isApproved": false,
              "isNew": true,
              "isoDate": "2017-04-17T13:08:41+04:00",
              "userID": "NfM26A2YcPUFz8rfYa23Kr3mjCO2",
              "userItchableName": "Alexsander K."
          }
      },
      "isNewPendingRequest": true,
      "isPrivateStatus": false,
      "isTest": false,
      "ownerID": "1Cix149ThIOG1ULPVjyy0LyTxbe2",
      "peopleProperties": {
          "availableSeats": 1,
          "numberOfPeople": 1,
          "numberOfPeopleDescription": "1 person"
      },
      "title": "Genius Lounge and Sake Bar",
      "userName": "Tim C.",
      "version": 1
  },

我也试过做这样的 link,但是如果从列表中删除任何卡片,这个块就会起作用。

    guard let ref = FIRDatabase.database().reference().child(MainGateways.cards.rawValue).child(card.id).parent else { return }

调查Cloud Functions for Firebase。使用它,您可以编写 JavaScript 代码来响应您指定路径上的数据库更改。这允许您编写独立于客户端应用程序的逻辑以响应数据库中的更改。