我收到一份奇怪的Firebase Crashlytics报告,我们无法复制或跟踪该问题,只有1.5%的用户收到了此崩溃,但我们需要修复它。
Crashed: com.apple.main-thread. EXC_BREAKPOINT 0x0000000100b057a4
SearchViewController.tableView(_:cellForRowAt:)
0 AppName 0x100b057a4 SearchViewController.tableView(_:cellForRowAt:) + 4309342116 (<compiler-generated>:4309342116)
1 AppName 0x100b05834 @objc SearchViewController.tableView(_:cellForRowAt:) + 4309342260 (<compiler-generated>:4309342260)
2 UIKitCore 0x211f3b618 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 680
3 UIKitCore 0x211f3bb18 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 80
4 UIKitCore 0x211f08320 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2260
5 UIKitCore 0x211f25640 -[UITableView layoutSubviews] + 140
6 UIKitCore 0x2121b4170 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1292
7 QuartzCore 0x1e9a6cc60 -[CALayer layoutSublayers] + 184
8 QuartzCore 0x1e9a71c08 CA::Layer::layout_if_needed(CA::Transaction*) + 332
9 QuartzCore 0x1e99d43e4 CA::Context::commit_transaction(CA::Transaction*) + 348
10 QuartzCore 0x1e9a02620 CA::Transaction::commit() + 640
11 QuartzCore 0x1e9a0315c CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 92
12 CoreFoundation 0x1e54c04fc __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
13 CoreFoundation 0x1e54bb224 __CFRunLoopDoObservers + 412
14 CoreFoundation 0x1e54bb7a0 __CFRunLoopRun + 1228
15 CoreFoundation 0x1e54bafb4 CFRunLoopRunSpecific + 436
16 GraphicsServices 0x1e76bc79c GSEventRunModal + 104
17 UIKitCore 0x211d1cc38 UIApplicationMain + 212
18 AppName 0x100a19654 main + 18 (RatingDetailsTableViewCell.swift:18)
19 libdyld.dylib 0x1e4f7e8e0 start + 4
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = list[indexPath.row]
switch selectedType {
case .book, .bundle, .gadget, .author:
let cell = tableView.dequeue(cellForItemAt: indexPath) as ItemTableViewCell
cell.configure(item)
return cell
case .none:
return UITableViewCell()
}
}
public func configure(_ item: Itemizable) {
self.item = item
// Adjest imageview shape depending on item
let isAuthor = ItemType.author == item.type
twoThirdRatioConstraint.isActive = !isAuthor
oneRatioConstraint.isActive = isAuthor
imgView.layer.cornerRadius = isAuthor ? imgView.bounds.width / 2 : 10
}
提前谢谢。。
更新1
添加数组列表&行数分区
fileprivate var list = [Itemizable]() {
didSet {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
我建议您添加一个基本保护。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// A good practice to prevent crashes is to always compare
// the number of objects and the indexPath.row
guard list.count > indexPath.row else {
// You could add a log here to know if that's your problem
return UITableViewCell()
}
let item = list[indexPath.row]
switch selectedType {
case .book, .bundle, .gadget, .author:
let cell = tableView.dequeue(cellForItemAt: indexPath) as ItemTableViewCell
cell.configure(item)
return cell
case .none:
return UITableViewCell()
}
}
有一个小窗口,您有一个用于numberOfRowsInSection
的新列表,但reloadData()
没有被调用,因为它是异步执行的。您应该做的是强制在主线程上执行列表的新值的分配,然后立即调用reloadData()
而不进行调度。