如何从Firebase获取特定数据

时间:2018-02-08 07:09:06

标签: swift firebase react-native firebase-realtime-database

[
  Beacons:[    
    Beacons1:[
        heritage:"Heritage Point1" 
        uuid:"qwerty"   
        ]   
    Beacons2:[
        heritage:"Heritage Point2"
        uuid:"asdfgh"
        ]
  ]

  Heritage Point1: [
          Title:"India"
          location:[
                lat:"11.345234"
                lon:"35.643521"
                ]
           Description:"Democratic Country"
  ]
]

这是Firebase模型! 我有uuid我需要从特定的遗产点获取数据1 来自我的Beacons Loop怎么可能...... 我知道只获取单个值。  这是我的代码!!

   'let rootRef = Database.database().reference()

    rootRef.child("Beacons").observe(.childAdded, with: {snapshot in

    guard let dictionary = snapshot.value as? [String: AnyObject] else { return }

   let uuidDatas = dictionary["uuid"] as? String

   let heritageDatas = dictionary["heritage"] as? String

        print("uuidData: \(uuidDatas ?? "")")
        print("HeritageData: \(heritageDatas ?? "")")

  })'

输出

uuidData: Optional("qwerty")
HeritageData: Heritage Point1
uuidData: Optional("asdfgh")
HeritageData: Heritage Point2

任何人请帮助我获取Heritage Point数据 我正在使用swift 4语言

1 个答案:

答案 0 :(得分:1)

请先修复您的问题格式,因为它很难阅读并理解您在代码中尝试执行的操作。

如果您想获取Firebase数据库中的特定项目,此示例可能会对您有所帮助:

// somebody try this if we can have space in-between Heritage and point1
let myDatabaseRef = Database.database().reference(withPath: "Beacons/Heritage Point1")

myDatabaseRef.observe(.value) { (snap:DataSnapshot) in 

    // Do whatever you want with your snapshot here
    print(snap.value)
}

或从他们的官方YouTube页面跟随本教程: https://www.youtube.com/watch?v=joVi3thZOqc&list=PLl-K7zZEsYLn-elkHPhDuuwdCZ93BXnrB&index=19