在Swift中使用Firebase实时数据库时如何获取Array数据?

时间:2022-09-08 21:26:04

I'm in a trouble when I get data from Firebase Realtime Database so I write this question.

当我从Firebase实时数据库获取数据时,我遇到了麻烦,所以我写了这个问题。

Here is the thing what I wanna do.

这就是我想做的事情。

This is data saved in Firebase database.

这是保存在Firebase数据库中的数据。

{ "schedule": 
  {"day0" : [{"title":"wake up", "content":"Wake up at 7 AM"},
             {"title":"School","content":"Go to the school"}],
   "day1" : [{"title":"day1 wakeup", "content":"Wake up at 8 AM"},
             {"title":"Supermarket", "content" :"buy some food"}]
   }
} 

In this case, I wanna get all the schedule in day0.

在这种情况下,我想在第0天获得所有的时间表。

[{"title":"wake up", "content":"Wake up at 7 AM"},
{"title":"School","content":"Go to the school"}]

So I make a code like this.

所以我做了这样的代码。

var ref:DatabaseReference!
ref = Database.database().reference()

ref.child("schedule").child("day0").observeSingleEvent(of: .childAdded) { (snapshot) in
  var postData = [NSDictionary]()
  self.dataList = (snapshot.value as? NSDictionary)!
  print("\(self.dataList)")
  self.tableView.reloadData()
}

And this is result. :(

这就是结果。 :(

{"title":"wake up", "content":"Wake up at 7 AM"}

How can I get all the data in the value of "day0"?

如何获取“day0”值的所有数据?

2 个解决方案

#1


0  

Right now you're observing a single .childAdded event. That means that you're only getting the first child node under day0.

现在你正在观察单个.childAdded事件。这意味着您只在day0下获得第一个子节点。

There are two ways to get all children:

有两种方法可以让所有孩子:

  1. observe all .childAdded events
  2. 观察所有.childAdded事件
  3. observe a .value event
  4. 观察.value事件

The first approach is simplest...

第一种方法最简单......

ref.child("schedule").child("day0").observe(.childAdded) { (snapshot) in
  print("\((snapshot.value as? NSDictionary)!)")
  self.tableView.reloadData()
}

With this minimal change, your completion handler will be called for each child node. Also see this example in the documentation on listening for child events.

通过此最小更改,将为每个子节点调用完成处理程序。另请参阅有关侦听子事件的文档中的此示例。

Alternatively you can observe .value (either once of continuously), which will be called with all matching data at once. This means that your code will need to handle the fact that it gets all data at once:

或者,您可以观察.value(连续一次),将立即调用所有匹配的数据。这意味着您的代码需要处理它一次获取所有数据的事实:

ref.child("schedule").child("day0").observe(.value) { (snapshot) in
  var postData = [NSDictionary]()
  self.dataList = (snapshot.value as? NSDictionary)!
  print("\(self.dataList)")
  self.tableView.reloadData()
}

Also see this example in the documentation to listening for value events.

另请参阅监听值事件的文档中的此示例。

#2


0  

var dataList: [Dictionary<String,Any>]?

ref.child("schedule").child("Day0").observeSingleEvent(of: .value) { (snapshot) in

self.dataList = snapshot.value as! [Dictionary]
self.tableView.reloadData()
}

This is result what I finally success I wanna do! Thank you for your help!

这是我最终成功的结果!感谢您的帮助!

#1


0  

Right now you're observing a single .childAdded event. That means that you're only getting the first child node under day0.

现在你正在观察单个.childAdded事件。这意味着您只在day0下获得第一个子节点。

There are two ways to get all children:

有两种方法可以让所有孩子:

  1. observe all .childAdded events
  2. 观察所有.childAdded事件
  3. observe a .value event
  4. 观察.value事件

The first approach is simplest...

第一种方法最简单......

ref.child("schedule").child("day0").observe(.childAdded) { (snapshot) in
  print("\((snapshot.value as? NSDictionary)!)")
  self.tableView.reloadData()
}

With this minimal change, your completion handler will be called for each child node. Also see this example in the documentation on listening for child events.

通过此最小更改,将为每个子节点调用完成处理程序。另请参阅有关侦听子事件的文档中的此示例。

Alternatively you can observe .value (either once of continuously), which will be called with all matching data at once. This means that your code will need to handle the fact that it gets all data at once:

或者,您可以观察.value(连续一次),将立即调用所有匹配的数据。这意味着您的代码需要处理它一次获取所有数据的事实:

ref.child("schedule").child("day0").observe(.value) { (snapshot) in
  var postData = [NSDictionary]()
  self.dataList = (snapshot.value as? NSDictionary)!
  print("\(self.dataList)")
  self.tableView.reloadData()
}

Also see this example in the documentation to listening for value events.

另请参阅监听值事件的文档中的此示例。

#2


0  

var dataList: [Dictionary<String,Any>]?

ref.child("schedule").child("Day0").observeSingleEvent(of: .value) { (snapshot) in

self.dataList = snapshot.value as! [Dictionary]
self.tableView.reloadData()
}

This is result what I finally success I wanna do! Thank you for your help!

这是我最终成功的结果!感谢您的帮助!