【问题标题】:How do I append data from a child item in Firebase database?如何从 Firebase 数据库中的子项附加数据?
【发布时间】:2017-01-10 11:12:44
【问题描述】:

如何在 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)
        }


    }

这是我的 Firebase 数据结构的图片:

数据应该只在ViewController 打开然后附加到数组中时获取一次。谢谢你的帮助。

编辑:目的是从firebase子“奖牌”获取数据并将其附加到本地数组“identifier2”

【问题讨论】:

    标签: ios xcode firebase firebase-realtime-database collectionview


    【解决方案1】:
    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)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多