如何将 FirebaseUI 与多个数据源一起用于表视图?
How do I use FirebaseUI with multiple datasources for a tableview?
FirebaseUI 上的文档很少。如何使用 FirebseUI 将不同的数据源用于表视图?
对于使用单个数据源的表视图,您可以使用FUIFirestoreTableViewDataSource
或FirebaseTableViewDataSource
。您可以使用查询(例如 FIRQuery
)将它们中的任何一个绑定到表视图,因此:
let query: Query = self.db.collection("users").whereField("name", isEqualTo: "Taiwo")
self.dataSource = self.tableView.bind(toFirestoreQuery: query) { tableView, indexPath, snapshot in
// get your data type out of the snapshot, create and return your cell.
}
这对于带有 'single source of truth' 的表视图非常有效,并且使您的表视图对数据库中的更改等做出反应,而无需您做太多工作。
但是,在需要动态更改数据的情况下(例如需要根据用户选择显示不同的数据),使用数据源将不起作用。在这种情况下,您需要使用来自 Firebase 的后备集合,例如 FUIBatchedArray
或 FUIArray
.
这与在 tableview 的数据源旁边使用 Swift 数组没有太大区别。唯一显着的区别是您通常需要使用 viewcontroller 作为其委托来初始化数组:
var datasourceArray = FUIBatchedArray(query: query, delegate: self)
然后
extension MyViewController: FUIBatchedArrayDelegate {
func batchedArray(_ array: FUIBatchedArray, didUpdateWith diff: FUISnapshotArrayDiff<DocumentSnapshot>) {
// you'll receive changes to your array in `diff` and `array` is a whole array with
// all new changes together with old data
datasourceArray = array
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
func batchedArray(_ array: FUIBatchedArray, willUpdateWith diff: FUISnapshotArrayDiff<DocumentSnapshot>) {
}
func batchedArray(_ array: FUIBatchedArray, queryDidFailWithError error: Error) {
}
}
然后您可以像在 UITableViewDataSource 方法中使用 Swift 数组一样使用 datasourceArray
:
extension MyViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datasourceArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let snapshot = datasourceArray.object(at: indexPath.row)
do {
let task = try snapshot.data(as: Task.self)
// create, customise and return your cell
}
}
catch {
print("coudln't get data out of snapshot", error.localizedDescription)
}
return UITableViewCell()
}
}
FirebaseUI 上的文档很少。如何使用 FirebseUI 将不同的数据源用于表视图?
对于使用单个数据源的表视图,您可以使用FUIFirestoreTableViewDataSource
或FirebaseTableViewDataSource
。您可以使用查询(例如 FIRQuery
)将它们中的任何一个绑定到表视图,因此:
let query: Query = self.db.collection("users").whereField("name", isEqualTo: "Taiwo")
self.dataSource = self.tableView.bind(toFirestoreQuery: query) { tableView, indexPath, snapshot in
// get your data type out of the snapshot, create and return your cell.
}
这对于带有 'single source of truth' 的表视图非常有效,并且使您的表视图对数据库中的更改等做出反应,而无需您做太多工作。
但是,在需要动态更改数据的情况下(例如需要根据用户选择显示不同的数据),使用数据源将不起作用。在这种情况下,您需要使用来自 Firebase 的后备集合,例如 FUIBatchedArray
或 FUIArray
.
这与在 tableview 的数据源旁边使用 Swift 数组没有太大区别。唯一显着的区别是您通常需要使用 viewcontroller 作为其委托来初始化数组:
var datasourceArray = FUIBatchedArray(query: query, delegate: self)
然后
extension MyViewController: FUIBatchedArrayDelegate {
func batchedArray(_ array: FUIBatchedArray, didUpdateWith diff: FUISnapshotArrayDiff<DocumentSnapshot>) {
// you'll receive changes to your array in `diff` and `array` is a whole array with
// all new changes together with old data
datasourceArray = array
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
func batchedArray(_ array: FUIBatchedArray, willUpdateWith diff: FUISnapshotArrayDiff<DocumentSnapshot>) {
}
func batchedArray(_ array: FUIBatchedArray, queryDidFailWithError error: Error) {
}
}
然后您可以像在 UITableViewDataSource 方法中使用 Swift 数组一样使用 datasourceArray
:
extension MyViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datasourceArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let snapshot = datasourceArray.object(at: indexPath.row)
do {
let task = try snapshot.data(as: Task.self)
// create, customise and return your cell
}
}
catch {
print("coudln't get data out of snapshot", error.localizedDescription)
}
return UITableViewCell()
}
}