2016-09-02 104 views
0

如何在Firebase數據庫中追加子項中的數據?以下是我的代碼。如何在Firebase數據庫中追加子項中的數據?

import UIKit 
import Firebase 
import FirebaseDatabase 

class MedalViewController: UICollectionViewController { 

    var imagesArray = [String]() 
    var identities = [String]() 
    var identities2 = [String]() //Creates an empty array 
    var imagesArrayGreyed = [String]() 
    let databaseRef = FIRDatabase.database().reference() 
    let userID = FIRAuth.auth()?.currentUser?.uid 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     imagesArray = ["1", "2", "3"] //Add all the medal images here 
     identities = ["Shield", "Tie", "Star"] //Add all the identities here 
     identities2 = ["Shield"] //Choose what medals the user has. 
     imagesArrayGreyed = ["1Greyed","2Greyed","3Greyed"] //Add all the greyed images here 

     databaseRef.child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in 
      // Get user value 
      var newIdentities2 = snapshot.value!["medals"] as! String 

      // how do I continue on from here? 


      // ... 
     }) { (error) in 
      print(error.localizedDescription) 
     } 


    } 

這是我的火力地堡數據結構的一個圖:

Image

數據應該被打開時ViewController只能使用一次獲得,然後附加到陣列。謝謝你的幫助。

編輯:其目的是從火力子獲得數據「獎牌」,並將它附加到本地陣列「identifier2」

回答

0
databaseRef.child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in 
     // Get user value 
     if let identities = snapshot.value! as? [String:AnyObject]{ 
       for each in identities.1 as String{ 
        identifier2.append(each)//will append medals 
       } 
     } 
     // ... 
    }) { (error) in 
     print(error.localizedDescription) 
    } 
相關問題