I build a todo app by using @SectionedFetchRequest to fetch the CoreData in IOS 15, I have two entity, one is "group" , the other is "Item", the relationship such as below:
the code is here:
// fetch request
@SectionedFetchRequest(
sectionIdentifier: \TodoListGroup.group,
sortDescriptors: [SortDescriptor(\TodoListGroup.date, order: .forward)],
animation: .default
) private var todos: SectionedFetchResults<String, TodoListGroup>
// view
ForEach(todos) { section in
Section(header:
HStack {
Text(section.id)
Spacer()
}) {
ForEach(section) { group in
ForEach(group.itemsArray) { item in
Text(item.content)
}
}
}
}
}
I can add, delete group or items ,it's no problem and the console debug show:
▿ 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"
but when i reopen my app in Xcode, the section.id changed ,like this:
▿ 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"
the id changed to another group's name, why ?