I am attempting to populate a 2D array (UICollectionView) of Square objects:
class Square: NSObject {
var sqrKey: String!
var userId: String!
init(SqrNumber: String, UserID: String) {
self._sqrKey = SqrNumber
self._userId = UserID
}
}
The ViewController is as such:
class CustomCollectionViewController: UICollectionViewController {
var ref: DatabaseReference!
var squares = [Square]()
override func viewDidLoad() {
super.viewDidLoad()
self.ref = Database.database().reference(withPath: "PlayerBoxes")
handle = ref.child("11062").observe(.value, with: { snapshot in
var items: [Square] = []
for item in snapshot.children {
let square = Square(snapshot: item as! DataSnapshot)
items.append(square)
}
self.squares = items
self.collectionView?.reloadData()
})
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 5
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
// Configure the cell
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CustomCollectionViewCell
cell.square = squares[(indexPath as NSIndexPath).item]
cell.label.text = cell.square?._sqrKey
return cell
}
}
I am running into two issues/error:
The first one is an issue more then an error which is that the Firebase read is not fully completed before the execution of the collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) function, which then causes the issue fatal error: Index out of range
at the line: cell.square = squares[(indexPath as NSIndexPath).item]
How do I get around that!? How do I force the Firebase read to complete before going forward!?
The second part of my question is this, I have put dummy data to ensure the error/issue mentioned above does not get encountered (by hard coding an array of Square objects) This ensures that the fatal error is not encountered and I can test the rest of my code; but when I do so the output of the result when executing the cell.label.text = cell.square?._sqrKey I get the same values for each item in each of the sections of the UICollection!?
so the output is:
0,1,2,3,4
0,1,2,3,4
0,1,2,3,4
0,1,2,3,4
0,1,2,3,4
I am expecting the output to be
0,1,2,3,4,
5,6,7,8,9,
10,11,12,13,14
15,16,17,18,19
20,21,22,23,24
Can anyone help me understand how I can index the correct _sqrKey values!?
The hard coded Square object is the key value being incremented by 1 and thus the results above.