Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

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!)
    })


Comment

PREVIOUS NEXT
Code Example
Swift :: share local storage wkwebview swift 
Swift :: how to present a uiview after an array of cards is empty swift 
Swift :: how to send a file from file manager in mail swift 
Swift :: declare variable in swif 
Swift :: tap to delete xcode 
Swift :: table view content size not return correctly 
Swift :: Type Constraints Swift 
Swift :: Create enum of Errors Swift 
Swift :: swift multiple return values 
Swift :: Swift Autoclosure 
Swift :: Swift is case-sensitive. So A and a are different variables 
Swift :: how to check if something is in a set in swift 
Swift :: implement swift protocol in kotlin 
Swift :: xcode create image from calayer 
Swift :: Swift Function With Argument Label 
Swift :: change textview link color swift 
Swift :: image copy swift extension 
Swift :: how request prefix of string in swift 
Swift :: Named Associated Values Swift 
Swift :: Swift Fatal error when accessing a null unwrapped optional 
Swift :: page view controller disable swipe 
Swift :: addition of numbers from array swift 
Ruby :: how to check if data is an array or not ruby 
Ruby :: ruby reorder keys in hash 
Ruby :: simple form for rails dates 
Ruby :: ruby capitalize first character of sentence 
Ruby :: rails g model 
Ruby :: rails form_tag 
Ruby :: convert to ascii ruby 
Ruby :: rails generate rake task 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =