簡體   English   中英

斯威夫特用戶界面。 長按 iOS 13.4-13.7 時,應用程序在 iPad 上崩潰

[英]SwiftUI. App crashes on iPad with iOS 13.4-13.7 when long-pressed

這是我關於stackoverflow的第一個問題。 如果我格式化主題有誤,請糾正我。

嘗試在真正的 iPad 上使用長按(?觸覺?)時,應用程序崩潰。 它也可以在 Simulators iPad 上重現,並在 Magic Trackpad 上單擊 Force Touch。

哪些設備有這個錯誤?

iOS 13.4到最后一個 iOS 13 的每個 iPad。 從iOS 13.4到最后一個 iOS 13 的每個模擬器 iPad。

我從未在 iPhone 上看到過這個錯誤。

我的測試設置:MacOS Catalina 10.15.6、 xCode 11.7 、Simulator iPad mini 2019 5-gen iOS 13.7、iPad 2018 6-gen iOS 13.7。

2020 年 10 月 26 日更新 此外,我嘗試在 MacOS Catalina 10.15.7、iPad 2018 6 代 iOS 13.7 上使用 xCode 12.0.1 我重新測試了,看起來這個錯誤在iOS 13.4-13.7上仍然可用,但在iOS 14 上已經消失了。 我沒有找到適用於 iOS 13 的好決定。

重現錯誤的步驟。

  1. 我將 SwiftUI 視圖( TestCrashIPAD )與 List、ForEach 和動態部分(下面的源代碼)一起使用。
  2. 一些靜態部分是預定義的,一些動態部分在從服務器獲取后出現。
  3. 重要的錯誤只有在第一次出現視圖時才能重現! 不要轉到另一個屏幕或最小化應用程序!
  4. 只需打開此視圖 ( TestCrashIPAD )。
  5. 等到動態部分出現。
  6. 部分出現后,嘗試長按帶有文本“r1”或“r2”或...的單元格(屏幕截圖)
  7. 崩潰(截圖)

碰撞

我在 AppDelegate 上崩潰了: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

我調查了一下,發現:這個錯誤自 iOS 13.4 以來可重現。 在 iOS 13.4 中還有一個View Modifier onDrag (link) 可能是以某種方式連接的,因為在調用堆棧中我看到:

UIDragInteractionLongPressDriver
UIGestureRecognizer

動態部分數組可能有問題,因為我看到:

SwiftUI.Sections.rowIDs(forSectionAt: Swift.Int)
SwiftUI.ListCoreDataSource.rowIndex(at: Foundation.IndexPath)

如何解決這個問題? 在這種情況下如何解決而不崩潰? 請說任何想法。

源代碼

請看一下源代碼:

import SwiftUI

struct TestCrashIPAD: View {

    @State var sections = [TestSection]()

    var body: some View {
        List {
            // predefined sections
            Section(header: Text("Section predefined")) {
                Text("1")
                Text("2")
            }
            Section(header: Text("Section predefined")) {
                ForEach(1..<7) {
                    Text("\($0)")
                }
            }

            // dynamic content
            ForEach(sections) { section in
                Section(header: Text(section.title)) {
                    ForEach(section.rows) { row in
                        HStack {
                            Text(row.str)
                            Spacer()
                        }
                    }
                }
            }
        }
        .listStyle(GroupedListStyle())
        .navigationBarTitle("iPad crash with long press")
        .onAppear(perform: onAppearAction)
    }

    func onAppearAction() {

        DispatchQueue.global().async {
            // fetch some data from the server
            sleep(3)

            // show data in UI
            DispatchQueue.main.async {
                self.sections = [TestSection](TestSection.array)
            }
        }
    }
}

struct TestSection: Identifiable {
    let id = UUID()
    let title: String
    let rows: [TestRow]

    static var array: [TestSection] {
        var result = [TestSection]()
        for idx in 0..<50 {
            result.append(TestSection(title: "s\(idx)", rows: TestRow.array))
        }
        return result
    }
}

struct TestRow: Identifiable {
    let id = UUID()
    let str: String

    static var array: [TestRow] {
        var result = [TestRow]()
        for idx in 0..<Int.random(in: 3..<7) {
            result.append(TestRow(str: "r\(idx)"))
        }
        return result
    }
}

看起來答案是將.onLongPressGesture { }添加到您崩潰的列表中(我從評論中得到了這個,但這解決了我的問題,所以似乎是一個答案。發布作為答案 bc 不是每個人都閱讀評論

暫無
暫無

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

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