Crash, UICollectionView: Snapshot when updating data.

I am able to statically load data and have it display in a CollectionView Diffable / Snapshot view without any problems. But I am getting the following crash ‘Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value’ when attempting to update the data from modal viewController.



I am using a shared instance pass data between the viewControllers, and I can use a print statement to see that the data is getting appended. I also tried to update the data on the main queue, but I still got the same error.


Here’s the code that I am using, and would appreciate any help.



func update(data: [Data]) {

var snapshot = NSDiffableDataSourceSnapshot<Section, Data>()



snapshot.appendSections([.main])

snapshot.appendItems(data)

self.dataSource.apply(snapshot, animatingDifferences: true, completion: nil)

}

Thanks,

Accepted Reply

I solved the issue.

The issue was that my delegates was not getting called by the correct VC.

Replies

Could you show the code where the crash occurs. And tell precisely where it occurs.


Also please show how you handle all var implied in the crash.

Sure. First, I thought that I knew why I was getting the 'force-unwrap' crash following the breakpoints for the crash in that I was using the following: var dataSource: UICollectionViewDiffableDataSource<Section, RandomData>!. So, I changed the code to typealias DataSource = UICollectionViewDiffableDataSource<Section, RandomData>.

I am still getting a "force-unwrap" crash. Here are both methods that I am seeing the respective crash when calling the method from a modal VC. NOTE: The data loads / displays fine on initial load, but crashes when attempting to add new data.

Method #1:
func updateData(for testData: [RandomData]) {
var snapshot = NSDiffableDataSourceSnapshot<Section, RandomData>()
Code Block
snapshot.appendSections([.main])
snapshot.appendItems(testData)

// Crash on dataSource apply method when calling from anotehr VC.
DispatchQueue.main.async {
self.dataSource.apply(snapshot, animatingDifferences: true, completion: nil)
}
Code Block
}

Method #2
func makeDataSource() -> DataSource {
// Crash on dataSource apply method when calling from anotehr VC.
let dataSource = DataSource(collectionView: collectionView) { (collectionView, indexPath, testData) -> UICollectionViewCell? in
//
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PerscriptionCell.reuseID, for: indexPath) as? RandomDataCell
cell?.set(dataType: testData)
return cell
Code Block
}
return dataSource
}

Thank you,
Paul


If you are seeing crashes it would be helpful if you could also post the stack trace of the crash together with your code and also point out the line in your code where you are seeing the crash.
0x7fff5108634a <+522>: callq  0x7fff510866d0            ; closure #1 (Swift.UnsafeBufferPointer<Swift.UInt8>) -> () in Swift.assertionFailure(: Swift.StaticString, _: Swift.StaticString, file: Swift.StaticString, line: Swift.UInt, flags: Swift.UInt32) -> Swift.Never
  • >  0x7fff5108634f <+527>: addq   $0x20, %rsp. - [Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value]

    0x7fff51086353 <+531>: ud2    

    0x7fff51086355 <+533>: movl   %r14d, %ecx

    0x7fff51086358 <+536>: shrl   $0x6, %ecx

    0x7fff5108635b <+539>: movl   %r14d, %eax

    0x7fff5108635e <+542>: andl   $0x3f, %eax

    0x7fff51086361 <+545>: shll   $0x8, %eax

    0x7fff51086364 <+548>: cmpl   $0x800, %r14d             ; imm = 0x800 

    0x7fff5108636b <+555>: jae    0x7fff51086391            ; <+593>

    0x7fff5108
Does Xcode highlight the line of code which produces the error in red? If so, could you post the code?
I solved the issue.

The issue was that my delegates was not getting called by the correct VC.