SwiftUI .onDelete 的列表视图崩溃应用程序
SwiftUI .onDelete of List view Crashes App
我有一个使用 SwiftUI 构建的相当简单的应用程序,它本质上是一个大师
细节类型。数据存储在核心数据中。添加和更新记录工作正常,
但从列表视图中删除项目时,应用程序总是崩溃。我正在使用
.onDelete 修饰符进行删除。我没有收到任何错误消息 - 只是
断线。记录确实被删除了,所以我猜测重新渲染
列表视图的 未接收更新数据。
我可能是在做梦,但我很确定删除功能在之前的版本中是有效的
两个贝塔。该应用程序仅在设备上运行。它不适用于预览版和模拟器。
iOS 13.1,Xcode (11392r),卡特琳娜 (19A546d)
这是 ContentView():
struct ContentView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(fetchRequest: ToDoItem.getAllToDoItems()) var toDoItems: FetchedResults<ToDoItem>
@State private var newToDoItem = ""
var body: some View {
NavigationView {
List {
Section(header: Text("Records")) {
ForEach(self.toDoItems) { toDoItem in
NavigationLink(destination: EditToDo(toDoItem: toDoItem)) {
ToDoItemView(title: toDoItem.title!,
firstName: toDoItem.firstName!,
lastName: toDoItem.lastName!,
//createdAt: "\(toDoItem.createdAt!)")
createdAt: self.localTimeString(date: toDoItem.createdAt!)
)
}
}
.onDelete { indexSet in
let deleteItem = self.toDoItems[indexSet.first!]
self.managedObjectContext.delete(deleteItem)
do {
try self.managedObjectContext.save()
} catch {
print(error)
}
}
.onMove(perform: move)
}
}
.navigationBarTitle("Customers")
.navigationBarItems(trailing: EditButton())
}
}
func move(from source: IndexSet, to destination: Int) {
print("this is the move method with no actions")
}
func localTimeString(date: Date) -> String {
let formatter = DateFormatter()
formatter.timeZone = .current
formatter.dateFormat = "M-d-yyyy HH:mm:ss"
let returnString = formatter.string(from: date)
return returnString
}//localTimeString
}
和托管对象:
public class ToDoItem : NSManagedObject, Identifiable {
@NSManaged public var id: UUID
@NSManaged public var createdAt: Date?
@NSManaged public var title: String?
@NSManaged public var firstName: String?
@NSManaged public var lastName: String?
}
extension ToDoItem {
static func getAllToDoItems() -> NSFetchRequest<ToDoItem> {
let request: NSFetchRequest<ToDoItem> = ToDoItem.fetchRequest() as! NSFetchRequest<ToDoItem>
let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: true)
request.sortDescriptors = [sortDescriptor]
return request
}
}
如有任何指导,我们将不胜感激。
这似乎难以置信,但显然将实体中的属性命名为 "id" 是导致此行为的原因。我将 UUID 属性的名称更改为 "myID" 并且删除现在有效。在进行更改之前,我将 Xcode 更新为 GM 种子 2(重命名之前,崩溃仍然发生在 GM2 中)。列表视图在预览中仍然完全不起作用,但它现在可以在模拟器和设备上使用。
我有一个使用 SwiftUI 构建的相当简单的应用程序,它本质上是一个大师 细节类型。数据存储在核心数据中。添加和更新记录工作正常, 但从列表视图中删除项目时,应用程序总是崩溃。我正在使用 .onDelete 修饰符进行删除。我没有收到任何错误消息 - 只是 断线。记录确实被删除了,所以我猜测重新渲染 列表视图的 未接收更新数据。
我可能是在做梦,但我很确定删除功能在之前的版本中是有效的 两个贝塔。该应用程序仅在设备上运行。它不适用于预览版和模拟器。
iOS 13.1,Xcode (11392r),卡特琳娜 (19A546d)
这是 ContentView():
struct ContentView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(fetchRequest: ToDoItem.getAllToDoItems()) var toDoItems: FetchedResults<ToDoItem>
@State private var newToDoItem = ""
var body: some View {
NavigationView {
List {
Section(header: Text("Records")) {
ForEach(self.toDoItems) { toDoItem in
NavigationLink(destination: EditToDo(toDoItem: toDoItem)) {
ToDoItemView(title: toDoItem.title!,
firstName: toDoItem.firstName!,
lastName: toDoItem.lastName!,
//createdAt: "\(toDoItem.createdAt!)")
createdAt: self.localTimeString(date: toDoItem.createdAt!)
)
}
}
.onDelete { indexSet in
let deleteItem = self.toDoItems[indexSet.first!]
self.managedObjectContext.delete(deleteItem)
do {
try self.managedObjectContext.save()
} catch {
print(error)
}
}
.onMove(perform: move)
}
}
.navigationBarTitle("Customers")
.navigationBarItems(trailing: EditButton())
}
}
func move(from source: IndexSet, to destination: Int) {
print("this is the move method with no actions")
}
func localTimeString(date: Date) -> String {
let formatter = DateFormatter()
formatter.timeZone = .current
formatter.dateFormat = "M-d-yyyy HH:mm:ss"
let returnString = formatter.string(from: date)
return returnString
}//localTimeString
}
和托管对象:
public class ToDoItem : NSManagedObject, Identifiable {
@NSManaged public var id: UUID
@NSManaged public var createdAt: Date?
@NSManaged public var title: String?
@NSManaged public var firstName: String?
@NSManaged public var lastName: String?
}
extension ToDoItem {
static func getAllToDoItems() -> NSFetchRequest<ToDoItem> {
let request: NSFetchRequest<ToDoItem> = ToDoItem.fetchRequest() as! NSFetchRequest<ToDoItem>
let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: true)
request.sortDescriptors = [sortDescriptor]
return request
}
}
如有任何指导,我们将不胜感激。
这似乎难以置信,但显然将实体中的属性命名为 "id" 是导致此行为的原因。我将 UUID 属性的名称更改为 "myID" 并且删除现在有效。在进行更改之前,我将 Xcode 更新为 GM 种子 2(重命名之前,崩溃仍然发生在 GM2 中)。列表视图在预览中仍然完全不起作用,但它现在可以在模拟器和设备上使用。