My code:
DataService.dataService.fetchDataFromServer { (channel) in
self.channels.append(channel)
let indexPath = IndexPath(item: self.channels.count - 1, section: 0)
self.collectionView?.insertItems(at: [indexPath])
}
Fetch Data From Server Function:
func fetchDataFromServer(callBack: @escaping (Channel) -> ()) {
DataService.dataService.CHANNEL_REF.observe(.childAdded, with: { (snapshot) in
let channel = Channel(key: snapshot.key, snapshot: snapshot.value as! Dictionary<String, AnyObject>)
callBack(channel)
})
}
Number of Items Section:
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 0
}
The Full Error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert item 0 into section 0, but there are only 0 items in section 0 after the update'
I am working with a collection view and have no clue why this error is showing up.
Any help would be appreciated!
Your numberOfItemsInSection
is returning 0, so when tell the collection view you are adding an item, it gets upset when this method says that there are still 0 items in the collection.
Your code even has a comment // #warning Incomplete implementation, return the number of items
You probably want something like:
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.channels.count
}