基本上,这是第一次运行它是完美的,然后第二次崩溃。我有一些图片显示了它在哪一行代码上崩溃。这个问题似乎与执行批处理有关。
代码:
let ref = Database.database().reference().child("test")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if let snapDict = snapshot.value as? [String:AnyObject] {
for snap in snapDict {
if snap.key == "testValue" {
self.log.performBatchUpdates({
let indexPath = IndexPath(row: group.count, section: 0)
group.append("textHere")
self.log.insertItems(at: [indexPath])
}, completion: nil)
}
}
最佳答案
您试图在一个循环中添加多个新值,但一次只能添加一个。最好将它们全部批量放入一个insert中。
let ref = Database.database().reference().child("test")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if let snapDict = snapshot.value as? [String:AnyObject] {
var newPaths = [IndexPath]()
for snap in snapDict {
if snap.key == "testValue" {
let indexPath = IndexPath(item: group.count, section: 0)
newPaths.append(indexPath)
group.append("textHere")
}
}
if !newPaths.isEmpty {
self.log.insertItems(at: newPaths)
}
}
}
我不使用Firebase,但据我所知,它的完成处理程序是在主队列上调用的。如果没有,则需要更新此代码,以便
if let snapDict...
中的此代码在主队列上运行。还要注意,在使用集合视图时,应该创建一个带有
IndexPath
和item
的section
。在表格视图中使用row
和section
。关于ios - 在UICollectionView Swift上第二次调用(插入项目)时,应用程序崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49291302/