Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get map from Firestore?

I want to implement a friendsystem.
Currently I am adding friends like this:

    public func addFriends(emailUser:String, emailFriend:String, completion: @escaping (Bool) -> Void) {
        
    let db = Firestore.firestore()
        
        if emailFriend == "" {
            print ("Failed to add friend")
            
            completion(false)
        }else {
    
        getUserData(email: emailFriend, isForFriend: true) { success in
            if success {
                
                db.collection("users").document(emailUser).setData([
                    "friends" : [[self.userDataOfMyFriends!.email]:[self.userDataOfMyFriends!.uid]]
                ], merge: true)
                
                completion(true)
            }else{
                print ("Failed to add friend")
                completion(false)
            }
        }
        }
    }

The Firebase data structure of the user looks like this:

email
 - "my@email.com"

friends
- 0
  - "myfriends@email.com": "AbCdeF1234567" // email:UID
uid
 - "AbCdeF1234567" //current users UID

And I am loading the profile data including friends of the current user like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  public func getUserData(email:String, isForFriend:Bool, completion: @escaping (Bool) -> Void) {
        let db = Firestore.firestore()
        let docRef = db.collection("users").document(email)
        print (email)
        
        docRef.getDocument { (document, error) in
            if let document = document, document.exists {
                let userEmail = document.get("email") as? String
                let userUID = document.get("uid") as? String
                let userFriends = document.get("friends") as? [[String:String]] // Here might be the problem
                if isForFriend {
                    
                self.userDataOfMyFriends = UserData(email: userEmail, uid: userUID, friends: userFriends)
                } else {
                self.userData = UserData(email: userEmail, uid: userUID, friends: userFriends)
                }
                print("userdata geladen")
                print (self.userData)
                completion(true)
            
                
            } else {
                
                
                completion(false)
            }
        }
    }

My UserDataModel looks like this:

public struct UserData {
    var email:String?
    var uid:String?
    var friends:[[String:String]]?

}

But the result is always nil.
How do I get the "map" data from Firebase in my UserDataModel?

>Solution :

So you need

let userFriends = document.get("friends") as? [String:String]

As friends is a dictionary not an array , hence

var friends:[String:String]?
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading