0

I have a web socket that can add or remove data to a MutableProperty<[Stuff]>

But when I reload the collectionView with collectionView.reloadData(). Web sockets add or remove data to the MutableProperty<[Stuff]>, so it will crash

for multiple causes here is some errors:

  • Index out of range when collectionView methods use my list
  • Invalid number of items cause crash like: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (58) must be equal to the number of sections contained in the collection view before the update (59), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

Anyone has a good advice or best practice to do that ? I've see that MutableProperty is thread safe

I know why it crash. It crash because websockets add or remove values from the array during the reloadData of the collectionView but I do not know how to solve it. I know we can use semaphore but I'm looking for something elegant

1 Answers1

0

You should show us a little more of your code so that we understand better. However, if you return a fixed value in delegates : numberOfSections and numberOfRowsInSection, you need to update them before calling collectionView.reloadData() so that these two delegate return the exact number of sections and items in each section.

chrisayn
  • 11
  • 1
  • 3
  • Thanks for your answer, the value is updated but when the relaod is in progress, the websocket can add data – uligerri-6156 Oct 29 '18 at 11:52
  • You should use a function that will add data, update all necessary variables and then finally reload the collection at the end – chrisayn Oct 29 '18 at 14:10