Hello,
I have been working on SwiftData since a month now and i found very weird that every time i update a data inside a SwiftData model my app became very slow. I used the instrument if something was wrong, and i see memory increasing without releasing.
My app have a list with check and unchecked elements (screeshot below).
I just press check and unchecked 15 times and my memory start from 149mb to 361mb (screenshots below).
For the code i have this model.
final class Milestone: Identifiable {
// *********************************************************************
// MARK: - Properties
@Transient var id = UUID()
@Attribute(.unique) var uuid: UUID
var text: String
var goalId: UUID
var isFinish: Bool
var createdAt: Date
var updatedAt: Date
var goal: Goal?
init(from todoTaskResponse: TodoTaskResponse) {
self.uuid = todoTaskResponse.id
self.text = todoTaskResponse.text
self.goalId = todoTaskResponse.goalId
self.isFinish = todoTaskResponse.isFinish
self.createdAt = todoTaskResponse.createdAt
self.updatedAt = todoTaskResponse.updatedAt
}
init(uuid: UUID, text: String, goalId: UUID, isFinish: Bool, createdAt: Date, updatedAt: Date, goal: Goal? = nil) {
self.uuid = uuid
self.text = text
self.goalId = goalId
self.isFinish = isFinish
self.createdAt = createdAt
self.updatedAt = updatedAt
self.goal = goal
}
}
For the list i have
var milestonesView: some View {
ForEach(milestones) { milestone in
MilestoneRowView(task: milestone) {
milestone.isFinish.toggle()
}
.listRowBackground(Color.backgroundComponent)
}
.onDelete(perform: deleteMilestone)
}
And this is the cell
struct MilestoneRowView: View {
// *********************************************************************
// MARK: - Properties
var task: Milestone
var action: () -> Void
init(task: Milestone, action: @escaping () -> Void) {
self.task = task
self.action = action
}
var body: some View {
Button {
action()
} label: {
HStack(alignment: .center, spacing: 8) {
Image(systemName: task.isFinish ? "checkmark.circle.fill" : "circle")
.font(.title2)
.padding(3)
.contentShape(.rect)
.foregroundStyle(task.isFinish ? .gray : .primary)
.contentTransition(.symbolEffect(.replace))
Text(task.text)
.strikethrough(task.isFinish)
.foregroundStyle(task.isFinish ? .gray : .primary)
}
.foregroundStyle(Color.backgroundComponent)
}
.animation(.snappy, value: task.isFinish)
}
}
Does someone have a idea?
Thanks