簡體   English   中英

滾動UITableView時發生隨機崩潰

[英]Random crash while scrolling UITableView

我有一個具有tableView的應用程序,該表可以包含數百個單元格,並且在與Xcode斷開連接時進行簡單滾動時出現隨機崩潰。

該表使用存儲在Realm中的對象數組。 除第0行外,大多數單元都是從該數組填充的,該行具有摘要並直接讀取Realm對象。

我正在嘗試復制崩潰(帶有重度忍者滾動)但未成功

這是代碼片段:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SummaryCell", for: indexPath) as! SummaryCell
            let count = "\(ItemsManager.shared.allActiveItems.count)" // ** Read from Realm **         
            cell.labellTotal.text = count
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell
            cell.labelTag.text = itemsArray[indexPath.row - 1]
            return cell
        }
    }

我將crashLog導入到Xcode / Devices中,並部分地進行了符號化,但是我不清楚如何發生。 (構建是在同一台Mac上完成的)

回溯顯示對CellForRow(atIndexPath :)的訪問,然后顯示對Realm的多個實例。 我所附的日志的最后一部分顯示了線程0崩潰,但這不是很有用(線程0崩潰:)

任何可能有問題的想法?

這是崩潰日志的摘要:

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  0

Application Specific Information:
abort() called

Filtered syslog:
None found

Last Exception Backtrace:
0   CoreFoundation                  0x184966d8c __exceptionPreprocess + 228
1   libobjc.A.dylib                 0x183b205ec objc_exception_throw + 55
2   Realm                           0x103af598c 0x1039d4000 + 1186188
3   Realm                           0x103af77c8 0x1039d4000 + 1193928
4   Realm                           0x103af7798 0x1039d4000 + 1193880
5   App Name Dev                    0x1027cd180 ItemsViewController.tableView(_:cellForRowAt:) + 1495424 (ItemsViewController.swift:274)
6   App Name Dev                    0x1027ce3ec @objc ItemsViewController.tableView(_:cellForRowAt:) + 1500140 (ItemsViewController.swift:0)
7   UIKit                           0x18e6400cc -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 667


Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libsystem_kernel.dylib          0x00000001843ed2ec __pthread_kill + 8
1   libsystem_pthread.dylib         0x00000001845926a8 pthread_kill$VARIANT$armv81 + 360
2   libsystem_c.dylib               0x000000018435bd0c abort + 140
3   libc++abi.dylib                 0x0000000183af72c8 __cxa_bad_cast + 0
4   libc++abi.dylib                 0x0000000183af7470 default_unexpected_handler+ 5232 () + 0
5   libobjc.A.dylib                 0x0000000183b208d4 _objc_terminate+ 35028 () + 124
6   libc++abi.dylib                 0x0000000183b1137c std::__terminate(void (*)+ 111484 ()) + 16
7   libc++abi.dylib                 0x0000000183b10f78 __cxa_rethrow + 144
8   libobjc.A.dylib                 0x0000000183b207ac objc_exception_rethrow + 44
9   CoreFoundation                  0x000000018482ce18 CFRunLoopRunSpecific + 664
10  GraphicsServices                0x0000000186811020 GSEventRunModal + 100
11  UIKit                           0x000000018e849758 UIApplicationMain + 236
12  App Name Dev                    0x0000000102702b6c main + 666476 (AppDelegate.swift:20)
13  libdyld.dylib                   0x00000001842bdfc0 start + 4

該問題可能是由於以下原因:

  1. 您在錯誤的線程中使用Realm
  2. 您與Realm同步工作,它阻塞了主線程
  3. 您正在使用Realm異步,並且如果單元正在重用並且操作仍在進行,則不要取消操作

一種可能是您使用強制轉換( as! )。 更改此行:

let cell = tableView.dequeueReusableCell(withIdentifier: "SummaryCell", for: indexPath) as! SummaryCell

至:

guard let cell = tableView.dequeueReusableCell(withIdentifier: "SummaryCell", for: indexPath) as? SummaryCell else {
  fatalError("Unable to dequeue cell with identifier 'SummaryCell'. Exiting.")
}

tableView.dequeueReusableCell()的其他調用進行相同的更改。 然后重新運行您的代碼,看看會發生什么。

如果強制轉換是問題,您仍然會崩潰,但是您會在控制台中看到一個非常明顯的錯誤,告訴您為什么崩潰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM