how to use firebase realtime database in swift ios code example
Example: swift firebase realtime db 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"
struct LocationStruct {
var coords: String?
var name: String?
var visibility: Bool?
}
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
}
}
ref.child("users").child("a_uid")
.observeSingleEvent(of: .value, with: { snapshot in
let user = UserClass(snap: snapshot)
print(user.email)
print(user.username)
for group in user.groupsDict {
print(group)
}
print(user.loc.coords!)
print(user.loc.name!)
print(user.loc.visibility!)
})