thatGuyNeil
thatGuyNeil

Reputation: 55

ios - Firebase writing complex data

Hi I'm new to Swift development and just started using Google's firebase database. I'm having some trouble constructing the JSON tree the way I want to, firstly because I'm using the .childByAutoId() func that creates a unique ID, which works great for now, but what if I wanted to name it to "Locations" in the db?

Secondly, my real issue is that I would like to have a nested array of Links inside each of my Locations, comprised of 2 strings. So I've tried using tuples but Firebase didn't like that. Was probably a silly solution. Anyhow I looked up firebase documentation and ended up with a bunch of compiling errors when trying to use it...

What would be the correct syntax for constructing a nested "Links" array in each of my "Locations", or basically an efficient way for creating a complex JSON in firebase. Here's my experimental code:

class func pushToFirebase(){

    let latitude = "33.333333"
    let longitude = "33.333333"
    let title = "foobar"
    let link = ("linkTitle","linkUrl")
    let link1 = "link"
    let links = [link, link1]

    let post:[String : AnyObject] = [
        "latitude":latitude,
        "longitude":longitude,
        "title":title,
        "links":links
    ]

    let firebaseRef = FIRDatabase.database().reference()
    firebaseRef.child("Locations").childByAutoId().setValue(post)   
}

desired structure

Upvotes: 0

Views: 2800

Answers (1)

Dravidian
Dravidian

Reputation: 9945

Always avoid using Tuples or Arrays in firebase database.Prefer using Dictionary To create your desired JSON tree :-

class func pushToFirebase(){

let latitude = "33.333333"
let longitude = "33.333333"
let title = "foobar"
let link = ("linkTitle","linkUrl")
let link1 = "link"
let links = [link, link1]

let post:[String : AnyObject] = [
    "latitude":latitude,
    "longitude":longitude,
    "title":title,
    "links":links
]
   //To create Locations 
let firebaseRef = FIRDatabase.database().reference()
firebaseRef.child("Locations").childByAutoId().setValue(post)  




  //To create create Links

func createLinks(linkTitle : String!,linkURL : String!){
      let selfUniqueGeneratedId = String(Int(NSDate.timeIntervalSinceReferenceDate() * 1000))
                 //Will give you a unique id every second it is called (or maybe millisecond too)

      firebaseRef.child("Locations").child("Links").child(selfUniqueGeneratedId).setValue([link_title : linkTitle,
      link_url : linkURL ])

   }   
}

You can not control Locations objects since they are created with childByAutoId that's why it has ..Auto.. in it... ;)

But surely you can read those AutoId with this code....

func retrieveFirebaseDB(){

 firebaseRef.child("Locations").observeEventType(.Value, withBlock: {(Snapshot) in

  if Snapshot.exists(){
          if let locationDict = Snapshot!.value as! [String:AnyObject] {
            for each in locationDict as [String:AnyObject]{
                  let locationID = each.0
               //Here you are retrieving every locationID in your node Locations, GO CRAZY...!
         }
       }
     }
  })

 }

Upvotes: 3

Related Questions