I am new to swift and developing my first application. In my application there is a collection view on which I want to display images. These images are retrieved from Firebase Database.
The structure for Firebase Database is as follows
{
"CHILD1" : {
"URL1" : "https://Child1_url1.com",
"URL2" : "https://Child1_url2.com",
"URL3" : "https://Child1_url3.com"
}
"CHILD2" : {
"URL1" : "https://Child2_url1.com",
"URL2" : "https://Child2_url2.com",
}
"CHILD3" : {
"URL1" : "https://Child3_url1.com",
}
}
To retrieve urls I am using below code
override func viewDidLoad() {
super.viewDidLoad()
checkSectionCount()
}
func checkSectionCount(){
Database.database().reference().observe(.value) { (snapShot: DataSnapshot) in
let snapDict = snapShot.value as? NSDictionary
let snapCnt = String(snapDict!.count)
print("snapCnt --> \(snapCnt)")// This prints the number of child counts correctly
}
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return snapCnt //I am unable to refer the snapCnt to set the number of section dynamically here
}
As indicated in above code, I am unable to refer snapCnt
out of func checkSectionCount
. I also tried to return this value as function parameter however swift is unable to identify snapCnt
out of
Database.database().reference(){ Code
}
Not sure what I am missing here. Appreciate if anyone can guide me through this on how to dynamically set the section and row numbers for collection view based on the details available in Firebase Database.