swift firebase realtime db class

//Firebase RealtimeDB Class
_ users
  |
  |_ a_uid
	 |
	 |_ email: "some email"
	 |
     |_ groups
  	 |	|_ group_1: true
     |  	|_ group_2: true
	 |
     |_ location
     |  |_ coords: "[52.5, 67.1]"
     |  |_ name: "Oyebanjo Solarin Street, Lagos"
     |  |_ visibility: true
	 |
     |_ username: "some username"

//1st
struct LocationStruct {
        var coords: String?
        var name: String?
        var visibility: Bool?
    }

//2nd
class UserClass {
    var email = ""
    var username = ""
    var groupsDict: [String: Any]
    var loc = LocationStruct()

    init(snap: FIRDataSnapshot) {

        let userDict = snap.value as! [String: Any]

        self.email = userDict["email"] as! String
        self.username = userDict["username"] as! String

        self.groupsDict = userDict["groups"] as! [String: Any]

        let locationDict = userDict["location"] as! [String: Any]
        self.loc.coords = locationDict["coords"] as? String
        self.loc.name = locationDict["name"] as? String
        self.loc.visibility = locationDict["visibility"] as? Bool
    }
}

//3rd
ref.child("users").child("a_uid")
                      .observeSingleEvent(of: .value, with: { snapshot in

        let user = UserClass(snap: snapshot)

        //this code is just to show the UserClass was populated.
        print(user.email)
        print(user.username)

        for group in user.groupsDict { //iterate over groups
            print(group)  //and print each one
        }

        print(user.loc.coords!) //print the location data
        print(user.loc.name!)
        print(user.loc.visibility!)
    })



Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source