Recently, I have been assigned to deal with crash reports and I found that there are a lot of crash report that come from 'NSInternalInconsistencyException' that are originated from 'UICollectionView'. The 2 reasons that were common are
'Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for number of items in section 0 when there are only 0 sections in the collection view'
I was not able to reproduce the crash. I'm sorry that I am not able to post the code or the full crash report, but I will try my best to briefly explain the situation.
I have a UIViewController
that is holding a UICollectionView
. The UIViewController
acts as the dataSource
and delegate
for the UICollectionView
. And the dataSource
and delegate
are assigned in the storyboard.
First, I thought that somehow the dataSource
disappear somewhere, which would have cause the crash. So I tried to force the crash by setting dataSource = nil
, but as expected it did not crash. I also found a solution here which says to check if dataSource
in layoutAttributesForElementsInRect
is equal to nil or deal with it accordingly. I tried to force dataSource = nil
in there, but still can't reproduce the problem (
UICollectionView and Supplementary View crash)
Is it possible that somehow the UIViewController
that acts as a dataSource
for the UICollectionView
died somehow so UICollectionView
does not have the dataSource? But wouldn't this be the same as UICollectionView.dataSource = nil
, which would not crash the app anyway?
Would it help if I separate UIViewController
and UICollectionViewDataSource
and then assign the dataSource
in the viewDidLoad
?
I would greatly appreciate it if someone could point me to the right direction in solving this problem.
Thanks
Edited: found a similar problem here iOS 9: UICollectionView dataSource is not set However, there is no solution yet.