Sean Calkins
Sean Calkins

Reputation: 322

Firebase query 2 levels deep without full reference swift

Is there a way to query firebase data without knowing the whole reference path? I need to be able to pull data from one user's profile into another based off of the solution number value.

Here is a picture of my data structure

data structure setup

I won't have access to another user's UID when I'm trying to send info. Is there any way around this or am I going to have to restructure my data?

This is my current query method, I just think the for loop might be bad practice

// MARK: - Search for trainees
func searchForTrainees() {

    self.ref.child("users").observeSingleEventOfType(.Value, withBlock: { snapshot in

        if snapshot.value! is NSNull {

        } else {

            if let users = snapshot.value! as? JSONDictionary {

                for user in users {

                    let userUID = user.0

                    if let userInfo = user.1 as? JSONDictionary {

                        if let uplineSolutionNumber = userInfo["upline_solution_number"] as? String {

                            if uplineSolutionNumber == self.currentUser?.solution_number {

                                self.syncUserData(userUID)

                            }
                        }
                    }
                }
            }
        }
    })
}

If there is a more efficient way to do this please let me know

Upvotes: 0

Views: 658

Answers (1)

Gil
Gil

Reputation: 569

You need to structure your data differently by duplicating data onto multiple nodes. Take a look at this article it should help you understand the concept: https://firebase.googleblog.com/2015/10/client-side-fan-out-for-data-consistency_73.html?m=1

Upvotes: 1

Related Questions