admin管理员组文章数量:1122846
In your app we are using a NavigationSplitView and SwiftData(with CloudKit) to display the data. When trying to delete items from the SwiftUi-List (using SwitUI EditButton)we are often confronted with unreproducible crashes that seem to be caused by the relationship between your models being used.
In the DetailView of the NavigationSplitView we show a list of Views (data held by SheetContent), only the last view in this list is editable. To keep track of the content in DetailView another model is used (SheetItem) which has a relationship with SheetContent and which is fetched via @Query in the View that builds the NavigationSplitView.
What happens in the case of a crash is that the data of the query seems not to be correctly updated after an item was deleted. We can see that when the list is rebuild also the just deleted SheetItem is still included but obviously the information about the related SheetContent is gone as the crash happens attempting to load the current editable one via a computed property. Here we return the last SheetContent in the list or create one if the list is empty which is the case when a new SheetItem is created. However the line of code where the crash happens differs when using an actual device or the simulator as you can see in the screenshot below.
On the simulator it fails attempting to read it from the now empty array in class SheetItem.
On the iPhone it tries to create a new SheetContent because of the empty list, failing then passing self which seems to be invalid by that time to the constructor of SheetContent.
So we are assuming some sort of timing problem here. Is there anything wrong with or code (see shortened version below) or any hints how to track this further down?
Help is very much appreciated
Screenshot simulator
Screenshot iPhone
struct SheetsNavigationView: View {
@Query(sort: \SheetItem.order) private var items: [SheetItem]
@Environment(\.modelContext) private var modelContext
@State var sheetsNavigationViewModel = SheetsNavigationViewModel()
var body: some View {
NavigationSplitView(columnVisibility: $sheetsNavigationViewModel.visibility) {
List(selection: $sheetsNavigationViewModel.selection) {
if items.isEmpty {
Button(action: addItem, label: {
Text("New sheet")
})
}
else {
ForEach(items, id: \.self) { item in
NavigationLink {
sheetView(for: item)
} label: {
Text("Sheet \(item.sheetName)")
}
}
.onDelete(perform: deleteItems)
}
}
} detail: {
navigationDestinationView()
}
}
private func deleteItems(offsets: IndexSet) {
for index in offsets {
if items[index].sheetNumber == displayedSheetNumber {
sheetsNavigationViewModel.selection = nil
}
modelContext.delete(items[index])
try? modelContext.save()
}
}
}
@Model
final class SheetItem: Identifiable {
var order: Int?
var sheetNumber: Int?
var sheetName: String?
@Relationship(deleteRule: .cascade, inverse: SheetContent.respectiveSheetItem)
var content: [SheetContent]?
var currentContentId: String?
var currentContent: SheetContent {
get {
if let currentContentId,
let foundContent = content?.first(where: { oneItem in
oneItem.contentId?.uuidString == currentContentId
}) {
return foundContent
}
else {
let newContent = SheetContent(respectiveSheetItem: self)
currentContentId = newContent.contentId?.uuidString
return newContent
}
}
}
}
@Model
final class SheetContent {
var contentId: UUID?
var respectiveSheetItem: SheetItem?
init(contentId: UUID? = UUID(), respectiveSheetItem: SheetItem? = nil) {
self.contentId = contentId
self.respectiveSheetItem = respectiveSheetItem
}
}
本文标签: swiftdataDeleting item with relationship from List causes crashStack Overflow
版权声明:本文标题:swiftdata - Deleting item with relationship from List causes crash - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308730a1933767.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论