我在IOS 15中使用@SectionedFetchRequest来获取CoreData构建了一个待办事项应用程序,我有两个实体,一个是“组”,另一个是“项目”,关系如下:
代码在这里:
// 获取请求
@SectionedFetchRequest(
sectionIdentifier: \TodoListGroup.group,
sortDescriptors: [SortDescriptor(\TodoListGroup.date, order: .forward)],
animation: .default
) private var todos: SectionedFetchResults<String, TodoListGroup>
// 看法
ForEach(todos) { section in
Section(header:
HStack {
Text(section.id)
Spacer()
}) {
ForEach(section) { group in
ForEach(group.itemsArray) { item in
Text(item.content)
}
}
}
}
}
我可以添加,删除组或项目,没问题,控制台调试显示:
▿ Section
▿ results : 1 element
- 0 : <TodoListGroup: 0x600001ca9220> (entity: TodoListGroup; id: 0x944fd1b9b585c7fc <x-coredata://670F71DD-39DA-47BD-987D-7075311C04A1/TodoListGroup/p6>; data: {
date = "2021-11-05 03:20:59 +0000";
group = SwiftUI;
items = (
"0x944fd1b9b405c7ec <x-coredata://670F71DD-39DA-47BD-987D-7075311C04A1/ToDoListItem/p10>",
"0x944fd1b9b425c7ec <x-coredata://670F71DD-39DA-47BD-987D-7075311C04A1/ToDoListItem/p11>"
);
})
- id : "SwiftUI"
但是当我在 Xcode 中重新打开我的应用程序时,section.id 发生了变化,如下所示:
▿ Section
▿ results : 1 element
- 0 : <TodoListGroup: 0x600001a2e6c0> (entity: TodoListGroup; id: 0xb1980459e3a37372 <x-coredata://670F71DD-39DA-47BD-987D-7075311C04A1/TodoListGroup/p6>; data: {
date = "2021-11-05 03:20:59 +0000";
group = SwiftUI;
items = "<relationship fault: 0x6000039185a0 'items'>";
})
- id : "Drawing"
id改成其他组的名字,为什么?