2

I am developing an iOS app using Swift 3 and Firebase. In this app I am creating a friend list with this structure:

Friends
 User_id
  Friend_user_id_1
  Friend_user_id_2

I want to get a list of friends for a specific user and this is possible but since I only store ids of friends I am missing Name and Email.

My idea is to do a second query for each user in the friend list to get their personal data. The option to this is to store the Name and Email along the friend_id but that feels cumbersome since the friend might change their Name or Email.

Friends
 User_id
  Friend_user_id_1
   Name
   Email

Is it ok making a second query to get user details on each loop or is that a performance killer?

Do I have to save the Name and Email in list too?

Update

This is how I solve it right now

func setupContent() {
        let user = FIRAuth.auth()?.currentUser
        if user != nil {
            DataService.dataService.FRIEND_REF.child((user?.uid)!).observe(.value, with: { snapshot in
                if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
                    for snap in snapshots {
                        DataService.dataService.USER_REF.child(snap.key).observeSingleEvent(of: .value, with: { (snapshot) in
                            let value = snapshot.value as? Dictionary<String, AnyObject>
                            let friend = Friend(key: snap.key, dictionary: value!)
                            self.friends.insert(friend, at: 0)
                            self.tableView.reloadData()
                        }) { (error) in
                            print(error.localizedDescription)
                        }
                    }

                }
            })
        }
    }

Thankful for all help before I start coding.

Community
  • 1
  • 1
Jonathan Clark
  • 19,726
  • 29
  • 111
  • 175
  • What is the purpose of the "Friends" object? Is it a list of "User_id"s? If it is, why? This structure seems paradoxical. If you could provide a little more detail about this, it would be helpful. – Benjamin Lowry Feb 02 '17 at 12:51
  • The purpose is to let the user add friends which is displayed in a list of friends. Right now I save the UID of each friend instead of the whole personal data to always display the latest personal data when listing friends. – Jonathan Clark Feb 02 '17 at 13:17
  • I have updated with my code – Jonathan Clark Feb 02 '17 at 13:51
  • "Is it ok making a second query to get user details on each loop or is that a performance killer?" Yes it's OK. No it isn't a performance killer. See http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786. – Frank van Puffelen Feb 02 '17 at 14:22
  • Not sure I understand the pipeline query. How can I refactor my query to pipeline? – Jonathan Clark Feb 02 '17 at 14:40
  • @FrankvanPuffelen So it is best practice to loop through each friend, and make an individual network call for each? – vikzilla Mar 22 '17 at 00:52
  • @FrankvanPuffelen Now that Cloud Firestore is here, should these types of data be stored in there instead of Realtime Database? – Ehtesham Hasan Feb 17 '18 at 12:46

0 Answers0