Swift collection view race condition while updating snapshot

Viewed 477

I am facing race condition while updating collectionview snapshot. Some of the images are missed or overwritten with following error.

2020-04-01 19:05:34.076250+0530 Image Upload - XEP0363[17022:1058871] [UIDiffableDataSource] Warning: 10 inserted identifier(s) already present; existing items will be moved into place for this current insertion. Please note this will impact performance if items are not unique when inserted. 2020-04-01 19:05:34.077237+0530 Image Upload - XEP0363[17022:1058871] [UIDiffableDataSource] Warning: 10 inserted identifier(s) already present; existing items will be moved into place for this current insertion. Please note this will impact performance if items are not unique when inserted.

    struct Photo {
      let id: String

      func hash(into hasher: inout Hasher) {
        hasher.combine(id)
      }

      static func ==(lhs: Photo, rhs: Photo) -> Bool {
        return lhs.id == rhs.id
      }
    }

    NetworkService.shared.fetchImages(category1)
      .receive(on: DispatchQueue.main)
      .sink(receiveValue: { [weak self] images in   // images: [Photo]
        guard let self = self else { return }
        var currentSnapshot = self.dataSource.snapshot()
        currentSnapshot.appendItems(images, toSection: category1)
        self.dataSource.apply(currentSnapshot, animatingDifferences: true)
      })
      .store(in: &subscriptions)

    NetworkService.shared.fetchImages(category2)
      .receive(on: DispatchQueue.main)
      .sink(receiveValue: { [weak self] images in      // images: [Photo]
        guard let self = self else { return }
        var currentSnapshot = self.dataSource.snapshot()
        currentSnapshot.appendItems(images, toSection: category2)
        self.dataSource.apply(currentSnapshot, animatingDifferences: true)
      })
      .store(in: &subscriptions)

0 Answers
Related