AntonBrock
@AntonBrock
screen light

Как решить ошибку Invalid update: invalid number of items in section 0 в collectionView на iOS12?

Весь текст ошибки:

Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (0) must be equal to the number of items contained in that section before the update (1), plus or minus the number of items inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).


Метод при котором возникает данная ошибка ( только при удаление, при создание или обновлении элемента из коллекции ошибки нет) , возникает данная ошибка только на iOS12 , на iOS13 и > ошибки нет.
var cards: [Card] = []

item = self.cards[itemIndex]
   self.collectionView.performBatchUpdates({
 // тут запрос к firebase
       self.db.collection(K.FStore.collectionName!).document((item?.idCard)!).delete() { err in
          if err != nil {
             print("err")
          } else {
// тут удаление из массива, который напрямую связан с CollectionView
             self.cards.remove(at: itemIndex)
          }
     }
}) {(finished) in
     self.collectionView.reloadData()
}


// метод для коллекции 
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {


 // !! Именно тут приложение падает. То есть метод выше полностью отрабатывает, но я не понимаю, как на ios 13 это работает, а на ios 12 нет.. 
       return cards.count 
}


При дебаге item есть, весь код отрабатывает.

Попытки решить проблему:
Нашел инфу тут: ссылка на SO

Попытался добавить reloadData() перед self.cards.remove(at: itemIndex), но понимаю, что глупо и , конечно, не решило проблему.

Надеюсь, тут помогут решить проблему, сам пока не смог додумать, спасибо!
  • Вопрос задан
  • 951 просмотр
Решения вопроса 1
AntonBrock
@AntonBrock Автор вопроса
screen light
Ответ был дан на SO.

Where are the batch updates (plural)?

You don't need performBatchUpdates, the method is only useful for multiple simultaneous updates. Delete the card in the database, on success remove the card from the data source array and delete the item – animated – in the collection view.

My suggestion assumes that the current index path is available which itemIndex derives from
item = self.cards[itemIndex]

self.db.collection(K.FStore.collectionName!).document((item?.idCard)!).delete() { err in
   if let error = err {
      print("An error occurred", error)
   } else {
      self.cards.remove(at: itemIndex)
      self.collectionView.deleteItems(at: [indexPath])
   }
}




Ссылка на SO: тут ответ

Конечно, не весь ответ помог, но именно из-за неверного метода у меня выходила ошибка.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы