I am using the below fuucntion to fetch Data and dispaly it. The problem is the view updates but does not sort according to id's (as I read online, @FetchRequest doesnt update view dynamically). It is only when I refresh my view, I see the sorting corrected. Is there any way out so that my view updates dynamically.
@FetchRequest (entity: ChatData.entity(), sortDescriptors: [NSSortDescriptor(key: "id", ascending: true)])
var chatData : FetchedResults <ChatData>
ScrollView {
VStack{
ForEach(chatData, id: \.self) { message in
if message.role != "system" {
ChatCell(message: message.content ?? "", user: message.role ?? "")
}
}
}//:VStack
.padding(.horizontal)
.listStyle(.plain)
}//:ScrollView
You can't use
idfor an attribute name because that is used for theIdentifiableprotocol you'll need to rename it to something else likeuid, and here is an improvement to the code using a predicate instead of anif: