看起來好像對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
}
},
...
}
}
如果同時users
和groups
是非常大的數據集,你會希望能夠有選擇地只下載一個特定的用戶所屬的組。 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,特別是如果你的查詢限制需求可以在沒有索引的情況下被滿足。
非常感謝您的好評! –
非常感謝我,這是我見過的最全面的答案之一:) – manmal