将UIImages添加到数组时崩溃:UnsafeMutablePointer.deinitialize具有负计数

时间:2019-06-16 20:17:16

标签: ios arrays swift multithreading swift4

这是我的源代码。我正在尝试将UIImages添加到数组数据源,并在完成时重新加载collectionview。但是偶尔,应用程序会在第self.imageDataArray.append(dimage)行崩溃,并显示错误-Thread 396: Fatal error: UnsafeMutablePointer.deinitialize with negative count

  func createUIImagesFromPathList(pathList: [String]) {
    let group = DispatchGroup()
    self.imageDataArray.removeAll()
    let first5 = pathList.prefix(5) //just take first 5 as thats what we are showing
    for path in first5 {
      group.enter()
      if let path = URL(string: path) {
        DispatchQueue.global(qos: .userInteractive).async {
          if let data = try? Data(contentsOf: path), let dimage =  UIImage(data: data) {
              self.imageDataArray.append(dimage)
            group.leave()
          }
        }
      }
    }
    group.notify(queue: .main) {
      self.reloadPhotosCollection?()
    }
  }

我应该使用串行队列而不是默认的全局并发队列吗?

0 个答案:

没有答案