Redstain
Redstain

Reputation: 187

How to grab Users personal list in Firebase to a tableview

I am trying to simply fetch a users favourite maps onto a tableview. something that i thought would be very basic but turned out to be extremely difficult.

The code here is the best that i have managed so far, Attempting to somehow reference a (users id) with a (yourmaps id) to fetch specific information.

For example. Since the user has made 1 map his favourite(with id (-LpY4XEER-b21hwMi9sp)). I want to look through all maps within root["yourmap"] and only fetch his map onto a tableview.

Firebase

"users" {
  "6g55cHXH4begwooHQvO4EKNV3xm1" : {
    "photoURL" : "https://firebasestorage.googleap...",
    "username" : "lbarri",
    "yourmaps" : {
      "-LpY4XEER-b21hwMi9sp" : true
    }
  }
}


"yourmaps": {
  "-LpY4XEER-b21hwMi9sp" : {
    "author" : {
      "photoURL" : "https://firebasestorage.googleapis.com/v...",
      "uid" : "6g55cHXH4begwooHQvO4EKNV3xm1",
      "username" : "lbarri"
    },
    "mapmoderators" : {
      "6g55cHXH4begwooHQvO4EKNV3xm1" : true
    },
    "mapphotoURL" : "https://firebasestorage.googleapis...",
    "mapusername" : "Hello World"
  },
  "-LpYo_pQ8zIOGHHlNU1Q" : {
    "author" : {
      "photoURL" : "https://firebasestorage.googleapis.com/v...3",
      "uid" : "RLFK9xnvhccTu2hbNHq0v05J2A13",
      "username" : "lbarri"
    },
    "mapmoderators" : {
      "RLFK9xnvhccTu2hbNHq0v05J2A13" : true
    },
    "mapphotoURL" : "https://firebasestorage.googleapis.com/...",
    "mapusername" : "Dream"
  }
}

Swift

    func getCurrentUserMaps() {
        guard let userProfile = UserService.currentUserProfile else { return }
        let currentUserId = userProfile.uid

        let userRef = Database.database().reference().child("users").child(currentUserId)
        userRef.observeSingleEvent(of: .value, with: { (snapshot) in

            let root = snapshot.value as? NSDictionary

            if let mapsByUser = root!["yourmaps"] as? [String: Bool] {
                for (documentId, status) in mapsByUser {
                    if status {
                        // Document is true, check for the maps
                        self.fetchyourmaps(key: documentId, owner: currentUserId)
                    }
                }
            }
        }) { (error) in
            print(error.localizedDescription)
        }
    }
    func fetchyourmaps(key:String, owner:String) {
        let yourMapRef = Database.database().reference().child("yourmaps")
        yourMapRef.observeSingleEvent(of: .value, with: {snapshot in
            let user = snapshot.value as? NSDictionary

            if let mapsByUser = user!["mapmoderators"] as? [String: Bool] {
                for (userId, status) in mapsByUser {
                    if userId == owner && status == true {
                        print("Owner \(owner) manages this \(user)")

                        var tempYourMap = [YourMapProfile]()
                        for key in (snapshot.value as? NSDictionary)! {
                            let childSnapshot = key as? DataSnapshot
                            let dict = childSnapshot!.value as? [String:AnyObject]                           
                            let author = dict!["author"] as? [String:AnyObject]
                            let uid = author!["uid"] as? String      
                            let username = author!["username"] as? String
                            let photoURL = author!["photoURL"] as? String
                            let url = URL(string:photoURL!)
                            let mapusername = dict!["mapusername"] as? String

                            let mapphotoURL = dict!["mapphotoURL"] as? String
                            let mapurl = URL(string:mapphotoURL!)

                            let userProfile = UserProfile(uid: uid!, username: username!, photoURL: url!, mapPoints: mapPoints!)
                            let yourmapprofile = YourMapProfile(mapid: childSnapshot!.key as! String, mapauthor: userProfile, mapusername: mapusername!, mapphotoURL: mapurl!)

                            tempYourMap.append(yourmapprofile)

                        }
                        self.yourmaps = tempYourMap
                        self.tableView.reloadData()
                    }
                }
            }
        })
    }

print("Owner \(owner) manages this \(user)") does print the correct maps onto the console

After that line it is when i cant figure out how to package the information to my tableview.

I have searched everywhere for information on how to retrieve data from Firebase when referencing one root folder to another but i cant find anything helpful. So any link/guide/ tutorial etc would be appreciated and i'll gladly take it from there. Is this at least how you are supposed to do it?

Upvotes: 0

Views: 47

Answers (1)

Jay
Jay

Reputation: 35677

There are a few ways to do this but here's two: Option 1 is to leverage a deep query to get the maps that are this users favorites. The second is to iterate over the users maps and pull each one at a time.

Option 1:

Start with a maps node like this

allMaps
   map_0
      favorite_of
         uid_0: true
         uid_3: true
      map_user_name: "Larry"
   map_1
      favorite_of
         uid_2: true
      map_user_name: "Moe"
   map_2
      favorite_of
         uid_0: true
      map_user_name: "Curly"

Then, a deep query to get all the favorite maps of uid_0

func queryToGetMyFavoriteMaps() {
    let uid = "uid_0"
    let ref = self.ref.child("allMaps")
    let path = "favorite_of/" + uid
    let query = ref.queryOrdered(byChild: path).queryEqual(toValue: true)
    query.observeSingleEvent(of: .value, with: { snapshot in
        for child in snapshot.children {
            print(child) //prints the map_0 & map_2 nodes since that's the favorite onces
        }
    })
}

Option 2

Change up the allMaps node since we won't be doing a query

allMaps
   map_0
      map_user_name: "Larry"
   map_1
      map_user_name: "Moe"
   map_2
      map_user_name: "Curly"

and then the users node will be something like this

users
   uid_0
      name: "Frank"
      favorite_maps:
         map_0: true
         map_2: true
   uid_1
      name: "Leroy"
      favorite_maps:
         map_1: true

and then the code that reads uid_0's favorite_maps node, and gets the keys from that snapshot, and then iterates over them, reading the map nodes one at a time.

func iterateToGetFavoriteMaps() {
    let uid = "uid_0"
    let userRef = self.ref.child("users").child(uid)
    userRef.observeSingleEvent(of: .value, with: { snapshot in
        if let mapRefs = snapshot.childSnapshot(forPath: "favorite_maps").value as? [String: Any] {
            let mapKeys = mapRefs.keys //note mapKeys is a Dict so we can directly access the keys
            for key in mapKeys {
                let mapRef = self.ref.child("allMaps").child(key)
                mapRef.observeSingleEvent(of: .value, with: { mapSnapshot in
                    let name = mapSnapshot.childSnapshot(forPath: "mapUserName").value as? String ?? "No Name"
                    print(name)
                })
            }
        }
    })
}

Upvotes: 1

Related Questions