如何在不从firebase获取值的情况下读取父节点名称对于ios?

时间:2018-05-07 04:53:38

标签: ios swift firebase

我想使用swift阅读exports.sendNotification = functions.firestore .document('chats/{chatID}') .onWrite((change, context) => { // Get an object representing the document console.log('chat triggered'); // perform desired operations ... // See documentation on defining a message payload. var message = { notification: { title: 'Hello World!', body: 'Hello World!' }, topic: context.params.chatID. //<- If you are using a CF version under v1.0 don't change here }; // Send a message to devices subscribed to the provided topic. return admin.messaging().send(message). //<- return the resulting Promise .then((response) => { // Response is a message ID string. console.log('Successfully sent message:', response); return true; //<- return a value }) .catch((error) => { console.log('Error sending message:', error); //return. <- No need to return here }); }); 名称restaurantBurger King

如何才有可能,任何人都可以帮助我,这是因为我的最终项目需要在1周内提交,但我对此失去了希望

enter image description here

Eating Point

2 个答案:

答案 0 :(得分:2)

只需使用以下代码即可获取父节点名称。

 ref = Database.database().reference()

 ref?.child("Restaurants").observe(.childAdded, with: { (snapshot) in
    let rest = snapshot.value as? String
// snapshot.key is give you parent node name
        print(snapshot.key)
    }
})

答案 1 :(得分:1)

仅使用以下代码获取餐馆名称。

dbReference = Database.database().reference()
dbReference?.child("Restaurants").observeSingleEvent(of: .value, with: {(snapshot) in
for rest in snapshot.children.allObjects as! [DataSnapshot] {
            print("Restaurant Name:\(rest.key)")
        }

})

使用StoryboardId传递所有数据使用下面的代码。

dbReference = Database.database().reference()
dbReference?.child("Restaurants").observeSingleEvent(of: .value, with: {(snapshot) in
for rest in snapshot.children.allObjects as! [DataSnapshot] {
            print("Restaurant Data:\(rest)")

        }

})

把它放在你的 并在destinationController中声明数据变量,如下所示:

var data = [DataSnapshot]()

你必须从didselectRow执行这样的讨论。

performSegue(withIdentifier: "segue", sender: self)

您可以从以下功能传递所选项目的数据。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let index = CategorytableView.indexPathForSelectedRow
let indexNumber = index?.row
print(indexNumber!)
let VC = segue.destination as! DestinationVC
VC.data = [rest] . //You can pass here entire data of selected row.

}