0

I have the structure of cards, there is a request for a card, it appears the property interestedUsers, this property contains an array of models in which various properties are stored, including the user id who sent this request.

I tried using query to access only those cards where the user added the card owner, so I needed to know the isApproved property, but I could not do it, because of the dynamic keys. Also I tried to get all the cards where the current user sent the request, either did not work, I either have all the cards, or the result is NSNull.

Please tell me how can I do this?

My structure

"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
  },

One of the options for my request

func getUserEvents() {
    let realmManager = RealmManager()
    guard let currentUserID = realmManager.getCurrentUser()?.id else { return }
    DispatchQueue.global(qos: .background).async {
        let ref = FIRDatabase.database().reference().child(MainGateways.cards.rawValue).queryOrdered(byChild: "\(Keywords.interestedUsers.rawValue)/\(currentUserID)/\(InterestedUserKeywords.userID)").queryEqual(toValue: currentUserID) // currentuser id

        ref.observeSingleEvent(of: .value, with: { (snapshot) in
            if snapshot.value is NSNull {
                debugPrint("snapshot is nsnull")
                return
            }
            debugPrint("getUserEvents", snapshot.value ?? "")
            guard let dict = snapshot.value as? [String : [String : Any]] else { return }
            debugPrint("dict count", dict.values.count, "---")
        }, withCancel: { (error) in
            debugPrint(error.localizedDescription)
        })
    }
}

I tried a lot of combinations of rules for the database, but as it said it did not lead me to the result, at the moment for the cards the rules are.

   "cards": {
        ".indexOn": ["ownerID", "interestedUsers"],
      },

Also I know that I can create a separate data structure where you can store a list of cards for each user, where it was confirmed, but I think that the first option is more correct. Thanks

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Alexander Khitev
  • 6,417
  • 13
  • 59
  • 115

0 Answers0