1

I'm a newbie at firebase I have implemented a sample app that able to transfer point to each other after transfer success I also added two fields called "sender_name" and "receiver_name" but it's too difficult to get all transitions based on user login I found sample ways to do just add multiple where to it, its work fine if true both but that's not what I want I want whereOr like SQL as an example below

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

any solution help, please

   func getUserTransition(){
        // process
        /*
         1.get all transition from tm_members sender and receiver by current user login 
         2.
         */
        guard let username = self.userSession?.username else {
            return
        }
        print("username in user session : \(username)")
        COLLECTION_TM_TRANSITIONS_UAT
            .whereField("sender_name", isEqualTo: username)
            .whereField("receiver_name", isEqualTo: username)
            .getDocuments { documentSnapshot, error in
            if error == nil {
                guard let value = documentSnapshot?.documents else { return }
           
                self.tmTransitions = value.map { (queryDocumentSnapshot) -> TmTransition in
                    let data = queryDocumentSnapshot.data()
                    let email = data["email"] as? String ?? ""
                    let is_sender = data["is_sender"] as? Bool ?? false
                    let point = data["point"] as? Int ?? 0
                    let username = data["username"] as? String ?? ""
                    let sender_id = data["sender_id"] as? String ?? ""
                    let receiver_id = data["receiver_id"] as? String ?? ""
                    let created_at = data["created_at"] as? Timestamp
                    let sender_name = data["sender_name"] as? String ?? ""
                    let receiver_name = data["receiver_name"] as? String ?? ""
                    print("username : \(email)")
                    return TmTransition(id: queryDocumentSnapshot.documentID, sender_id: sender_id, receiver_id: receiver_id, username: username, is_sender: is_sender, point: point, email: email,created_at: created_at,sender_name: sender_name,receiver_name: receiver_name)
              
                }
              
            }
            else{
                print("error during fetch data ")
            }
        }
    }
Jay
  • 34,438
  • 18
  • 52
  • 81
  • Does this answer your question? [iOS - OR query in Firebase](https://stackoverflow.com/questions/52758235/ios-or-query-in-firebase) – Matt U May 25 '21 at 17:03
  • 2
    @MattU That article is for the Realtime Database. This question uses Cloud Firestore where there are a number of options for an OR style query. They may not be in the traditional sense but they can be done. Start with [in, not-in, and array-contains-any](https://firebase.google.com/docs/firestore/query-data/queries#in_not-in_and_array-contains-any). If you were to add another field called `sender_receiver` that contains an array `[sender_ui, receiver_uid]` that could then be queried using and in query like this `ref.whereField("sender_receiver", in: ["uid_0", "uid_1"])` – Jay May 25 '21 at 17:14
  • @Jay thank you for replying I got your idea I will try it – Noch PhaNith May 25 '21 at 17:26
  • Yikes, thanks @Jay. I was clearly not paying enough attention. – Matt U May 25 '21 at 17:26
  • 'Firebase' doesn't ring me a bell unless you specify which service you are referring to. – El Tomato May 26 '21 at 01:30
  • Hey there! Could you please tell me if you were able to solve this? – Samuel Romero Sep 06 '21 at 15:17

0 Answers0