M. Kremer
M. Kremer

Reputation: 679

Problems with structuring and retrieving data from Firebase in Swift

I am designing a little quiz app but I'm having trouble while retrieving the game data.

data structure

As you can see in the picture I have an JSON object that contains many single games. Each single game has a unique id. My first problem is that each of the games can be available in multiple languages. I know that I could download the hole snap and then looping throw each game but that would mean really long loading times while the app is growing.

In short form:

I need to retrieve the following data from the JSON above:

If it is easier to restructure the data in the JSON, please tell me.

Thanks for helping me.

Upvotes: 1

Views: 218

Answers (1)

Dravidian
Dravidian

Reputation: 9955

Answer to your first part :-

 let enRef = FIRDatabase.database().reference().child("singleGames").child(singleGamesUID).child("en")

  enRef.observeEventType(.Value, withBlock: {(snap) in
  if let enQuizQuestion = snap.value as? [String:AnyObject]{
          //Question exists : Retrieve Data 

      }else{

        //Question in english doesn't exist
    }
})

For your second part Since you are trying to save iteration time might i suggest you also save your singleGames id in a separate languagesBased nodes, there is a command in firebase that allows you to search for some keyValues in your child node's , but even that i think would be executing a search algorithm which might be a little more time consuming :--

appServerName:{
  singleGames :{
      uid1:{......
            ......
               ...},
      uid2:{......
            ......
               ...},
      uid3:{......
            ......
               ...}
           },
   enQuestions:{
       uid3 : true
          }
   deQuestions:{
       uid1 : true,   
       uid2 : true
         }
     }

Now all you gotta do :-

let rootRef = FIRDatabase.database().reference().child("deQuestions").observeEventType(.Value, withBlock: {(qSnap) in

    if let qDict = qSnap.value as? [String:AnyObject]{
            for each in qDict as [String:AnyObject]{
              let deUID = each.0
          }
      }else{

      //No question in dutch language
     }
   })

Upvotes: 1

Related Questions