juelizabeth
juelizabeth

Reputation: 505

retrieve unique data from firebase

I have some information being sent to firebase. In my app, users enter info about themselves and it's stored under the uids. Here is the hierarchy:

{
  "gJRhIOfmckTq9cRWNnwhg7tdxxv1" : {
    "City" : "New York, New York",
    "College" : "New York University of New York",
    "Major" : "Nursing",
    "uid" : "gJRhIOfmckTq9cRWNnwhg7tdxxv1"
  },
  "ix7JSoW5JZMSmDjwQRUUd8W7fi2" : {
    "City" : "Miami, Florida",
    "College" : "University of Miami",
    "Major" : "Neuroscience ",
    "uid" : "ix7JSoW5JZMSmDjwQRUUd8W7fi2"
  }
}

Here is the path to uploading the data:

FIRDatabase.database().reference().child("info").child(self.loggedInUser!.uid)
  .observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in

The issue is that when a particular user is logged in, they should be able to see other user's information but when I try to retrieve the not logged in user's information, I run into a problem because of the uid. I get only the logged in user's information displayed in the non-logged in user information page. How can I make .child(self.loggedInUser!.uid)so that it does not only capture the logged in user's uid but all uids.

Upvotes: 0

Views: 392

Answers (1)

Jen Person
Jen Person

Reputation: 7546

Based on your response to my comment, it seems like you need all of the information about a user when, say, their profile is selected. This is where denormalization will come in handy. This blog post and this YouTube video go into detail on the what and why of denormalization, so I won't get into that too much here.

Using denormalization, you could include a child that lists all users with a username as a key and the user's uid as the value. Then when a user selects that user by username, say from a list in a table, you can find the user's uid and then listen to the value of the user's profile. The database structure would be something like this:

{
    users: {
        "thisperson": "gJRhIOfmckTq9cRWNnwhg7tdxxv1",
        "anotherperson": "ix7JSoW5JZMSmDjwQRUUd8W7fi2",
        ...
    },
    info: {
        "gJRhIOfmckTq9cRWNnwhg7tdxxv1" : {
            "City" : "New York, New York",
            "College" : "New York University of New York",
            "Major" : "Nursing",
            "uid" : "gJRhIOfmckTq9cRWNnwhg7tdxxv1"
        },
        "ix7JSoW5JZMSmDjwQRUUd8W7fi2" : {
            "City" : "Miami, Florida",
            "College" : "University of Miami",
            "Major" : "Neuroscience ",
            "uid" : "ix7JSoW5JZMSmDjwQRUUd8W7fi2"
        },
        ...
    }
}

Then instead of listening to the values for the logged in user, you listen for the value of the selected user's uid, then listen using that instead:

 FIRDatabase.database().reference().child("info").child(otheruid)
   .observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in

Where otheruid is the value of FIRDatabase.database().reference().child("users").child(username)

Upvotes: 0

Related Questions