由于目前 FirebaseUI 的 FUICollectionDataSource
处理 UI 的所有更改,因此我正在使用 FUIArray
并观察数据源的更改以及手动更新我的 collectionView。然而,再看一眼,我相信我应该实现 FUIIndexArray
,因为它的排序行为。但是,我不确定这是两个初始化查询。
From the docs:
- @param index A Firebase database query whose childrens' keys are all children of the data query.
- @param data A Firebase database reference whose children will be fetched and used to populate the array's contents according to the index query.
对于数据参数,我使用与 FUIArray
相同的查询(有效)。对于索引参数,我不确定要使用什么,所以我使用相同的查询来测试,它似乎只工作了一半。在这两个查询中,我都按优先级排序。如果我检查 FUIIndexArray
的长度,我会得到正确的计数,但当我检查其 .items
时,数组为空。
FUIIndexArray
是否应该与 FUIArray
类似地工作,只是它也返回一个索引?索引查询应该是什么样子?
最佳答案
看起来对于 FUIIndexArray
的实际用途存在一些困惑。 FUIIndexArray
使用索引查询子项的键从数据查询中加载子项,这意味着每个元素加载都是异步的,如果没有,.items
将不会返回任何内容这些加载已完成(尽管这里可能应该返回一个充满 NSNull
的数组)。
FUIIIndexArray
旨在通过查询来准确指定应加载哪些元素,从而更轻松地加载大型数据集的非常精细的部分。假设您的数据库如下所示:
// An index to track Ada's memberships
{
"users": {
"alovelace": {
"name": "Ada Lovelace",
// Index Ada's groups in her profile
"groups": {
// the value here doesn't matter, just that the key exists
"techpioneers": true,
"womentechmakers": true
}
},
...
},
"groups": {
"techpioneers": {
"name": "Historical Tech Pioneers",
"members": {
"alovelace": true,
"ghopper": true,
"eclarke": true
}
},
...
}
}
如果用户
和组
都是非常大的数据集,您将希望能够有选择地仅下载特定用户所属的组。 FUIIndexArray
正是解决了这个用例(仅此而已)。
let adasGroups = database.reference(withPath: "users/alovelace/groups")
let allGroups = database.reference(withPath: "groups")
// array will load only alovelace's groups
let array = FUIIndexArray(index: adasGroups, data: allGroups, delegate:self)
由于每个元素必须单独加载,FUIIndexArrayDelegate
提供了一个回调来单独处理每个加载,并实现此回调以正确处理成功加载和错误,可以增加代码库的分形复杂性。您应该尽可能避免使用 FUIIndexArray,而坚持使用更简单的 FUIArray,特别是如果您的查询限制需求可以在没有索引的情况下得到满足。
关于ios - FirebaseUI-iOS FUIIndexArray 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41222937/