我正在尝试从Firebase数据库中的所有用户获取所有相同的子值,并且遇到了一些问题。在我的特定用例中,我希望所有用户的所有“Planning”/“planning”子值都加载到collectionView中。我已经能够直接手动映射到子项“Planning”的值,但我不得不使用currentUser.uid,这当然只给了我当前登录用户的“规划”值(所以不是所有用户“规划” “价值观”。同样,我想在我的整个Firebase数据库中加载带有所有“Planning”值的collectionView。在此先感谢您的帮助......我已经坚持了一段时间!
//来自Firebase的JSON
{
"users" : {
"CmtuebwVrmOG3n4uSsIK1b4FsSN2" : {
"Planning" : {
"-Kl_xoIEXw1HqbL9-JDA" : {
"addedBy" : "CmtuebwVrmOG3n4uSsIK1b4FsSN2",
"planning" : "Napa Valley, CA, USA",
"timestamp" : 1.49635932283732E9
}
},
"name" : "Daniel Meier",
},
"TFN8NFSPfdMBJQ15iDhhRSRVUcV2" : {
"Planning" : {
"-KlW0Tw3ZMnTDvHgN-NM" : {
"addedBy" : "TFN8NFSPfdMBJQ15iDhhRSRVUcV2",
"planning" : "Patagonia",
"timestamp" : 1.496276397864224E9
},
"-KlW0nqdABHlMa7Lj31X" : {
"addedBy" : "TFN8NFSPfdMBJQ15iDhhRSRVUcV2",
"planning" : "Cabo San Lucas, Baja California Sur, Mexico",
"timestamp" : 1.496276483532304E9
},
"-KlW1sc4t-jUKqXxQpuf" : {
"addedBy" : "TFN8NFSPfdMBJQ15iDhhRSRVUcV2",
"planning" : "Vancouver, BC, Canada",
"timestamp" : 1.496276765230323E9
},
"-KlW4aCDUXKBqnxKoU04" : {
"addedBy" : "TFN8NFSPfdMBJQ15iDhhRSRVUcV2",
"planning" : "Santiago, Santiago Metropolitan Region, Chile",
"timestamp" : 1.496277476204581E9
},
"-KlW6BQpoO4wIWsTJcnC" : {
"addedBy" : "TFN8NFSPfdMBJQ15iDhhRSRVUcV2",
"planning" : "Istanbul, İstanbul, Turkey",
"timestamp" : 1.496277894928425E9
},
"-KlWFHKBKrEd-z8qM9gz" : {
"addedBy" : "TFN8NFSPfdMBJQ15iDhhRSRVUcV2",
"planning" : "Lyon, France",
"timestamp" : 1.496280278376963E9
},
"-Kl_wL42PfG_MtvkwWpk" : {
"addedBy" : "TFN8NFSPfdMBJQ15iDhhRSRVUcV2",
"planning" : "Kentucky, USA",
"timestamp" : 1.496358936906602E9
}
},
"name" : "Dan Meier",
},
"jtnJjBCW9mdkvXy7DV7ox1DUvzO2" : {
"Planning" : {
"-Kl_xgDG-7CpdQyvrbM5" : {
"addedBy" : "jtnJjBCW9mdkvXy7DV7ox1DUvzO2",
"planning" : "Hong Kong Island, Hong Kong",
"timestamp" : 1.496359289751177E9
},
"-Kl_xi0vCO-BZ10AMua3" : {
"addedBy" : "jtnJjBCW9mdkvXy7DV7ox1DUvzO2",
"planning" : "London, UK",
"timestamp" : 1.496359297153533E9
}
},
"name" : "Daniel Meier",
}
}
}
// NSObject模态对象
class CurrentPlanner: SafeJsonObjectPlanner {
var planningPlace: String?
init(dictionary: [String: AnyObject]) {
super.init()
self.planningPlace = dictionary["planning"] as? String
setValuesForKeys(dictionary)
}
}
// baseCell CollectionViewCell类
class BaseCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setupViews() {
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//用于从Firebase
获取计划值的功能var plannedPlaces = [CurrentPlanner]()
func fetchPlanningPlaces() {
let baseRef = Database.database().reference().child("users")
baseRef.observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let planningCell = CurrentPlanner(dictionary: dictionary)
self.plannedPlaces.append(planningCell)
print(self.plannedPlaces)
}
}, withCancel: nil)
//this will crash because of background thread, so lets use dispatch_async to fix
DispatchQueue.main.async(execute: {
self.collectionView.reloadData()
})
}
// setup collectionView
override func setupViews() {
super.setupViews()
fetchPlanningPlaces()
// ...
collectionView.register(BasePlanningCell.self, forCellWithReuseIdentifier: cellId)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return plannedPlaces.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! BasePlanningCell
cell.currentPlanner = plannedPlaces[indexPath.item]
return cell
}