1

我正在尝试使我的应用程序内容保持反应性并与远程数据保持同步。我的价值观是使用 a 来探索的NavigationLink,但是当我深入 2 页并且原始项目消失时,SwiftUI 会崩溃。

开始:

class App: ObservableObject {
    @Published var items: [Item] = [
        Item(id: "a", accounts: [
            Account(id: "1"),
            Account(id: "2"),
            Account(id: "3")
        ])
    ]
}
struct AllItems: View {
    @EnvironmentObject var app: App
    var body: some View {
        ForEach(app.items.indices, id: \.self) { index in
            NavigationLink(destination: ItemView(item: self.$app.items[index])) {
                Text(self.app.items[index].id)
            }
        }
    }
}
struct ItemView: View {
    @Binding var item: Item
    var body: some View {
        ForEach(item.accounts.indices, id: \.self) { index in
            NavigationLink(destination: AccountView(item: self.$item.accounts[index])) {
                Text(self.item.accounts[index].id)
            }
        }
    }
}
struct AccountView: View {
    @Binding var account: Account
    var body: some View {
        Text(account.id)
    }
}

但是,如果我在 AccountView 页面上并items变为空,则应用程序将因“致命错误:索引超出范围”而崩溃。

我最初没有使用绑定,但是当 Account 结构的内容发生更改时,AccountView 没有更新,视图没有更改。现在通过传递绑定,视图确实会正确更新,但如果列表大小发生变化,它可能会导致崩溃。

4

2 回答 2

0

以下测试代码显示了一种可能的解决方法,以防止在项目变空时崩溃。

class Item {
let id: String
var accounts: [Account]
init(id: String, accounts: [Account]) {
    self.id = id
    self.accounts = accounts
}
}

struct Account {
let id: String
}

class App: ObservableObject {
@Published var items: [Item] = [
    Item(id: "item a", accounts: [
        Account(id: "account 1"),
        Account(id: "account 2"),
        Account(id: "account 3")
    ])
]
}

struct ItemView: View {
@EnvironmentObject var app: App
@State var itemNdx: Int
var body: some View {
    itemNdx < self.app.items.count
        ? AnyView(ForEach(app.items[itemNdx].accounts.indices, id: \.self) { index in
            NavigationLink(destination: AccountView(itemNdx: self.itemNdx, accountNdx: index)) {
                Text(self.app.items[self.itemNdx].accounts[index].id)
            }
        })
        : AnyView(EmptyView())
}
}

struct AccountView: View {
@EnvironmentObject var app: App
@State var itemNdx: Int
@State var accountNdx: Int
var body: some View {
    itemNdx < self.app.items.count
        ? AnyView(Text(app.items[itemNdx].accounts[accountNdx].id))
        : AnyView(EmptyView())
}
}

struct AllItems: View {
@EnvironmentObject var app: App
var body: some View {
    ForEach(app.items.indices, id: \.self) { index in
        NavigationLink(destination: ItemView(itemNdx: index)) {
            Text(self.app.items[index].id)
        }
    }
}
}

struct ContentView: View {
// the app is passed in from the SceneDelegate "var app = App()"
@EnvironmentObject var app: App

var body: some View {
    NavigationView {
        AllItems()
    }.navigationViewStyle(StackNavigationViewStyle())
    .onAppear(perform: loadData)
}

func loadData() {
    // this will set the items to empty in 10 seconds
    DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
        self.app.items = []
    }
}
}
于 2020-04-12T03:02:04.970 回答
-1

是的,如果在您的代码的另一部分中项目变为空,则应用程序将崩溃。当涉及到并发/多线程/异步等......你必须自己照顾它。例如,使用 GCD 框架或组合函数式反应库来获得正在发生的事情的通知并采取行动。

于 2020-04-12T01:30:12.083 回答