Reputation: 91
I have a small practice todo app I am building to learn SwiftUI, and I am able to create a list of tasks in CoreData and mark them as complete which sets isComplete to true and changes the image from an empty circle to a checkmark.
List {
ForEach(taskItems, id: \.self) { item in
HStack {
if item.isComplete == true {
Image(systemName: "checkmark.circle.fill").padding(5.0).foregroundColor(/*@START_MENU_TOKEN@*/.blue/*@END_MENU_TOKEN@*/).font(.system(size: 24))
.onTapGesture {
item.isComplete.toggle()
self.saveTasks()
}
} else {
Image(systemName: "circle").padding(5.0).foregroundColor(/*@START_MENU_TOKEN@*/.blue/*@END_MENU_TOKEN@*/).font(.system(size: 24))
.onTapGesture {
item.isComplete.toggle()
self.saveTasks()
}
}
Text("\(item.title)")
}
.padding(.vertical, 5.0)
}
.onDelete(perform: deleteTask)
}
However, I want these items to be removed from the list once the user has left the view (like the native iOS reminders app). I have a line of code in my fetch request that does remove the items from the list, but it removes them immediately.
@FetchRequest(
entity: TaskItem.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \TaskItem.order, ascending: false)]
predicate: NSPredicate(format: "isComplete == %@", NSNumber(value: false))
) var taskItems: FetchedResults<TaskItem>
On tap of the item images, how can I mark the item as complete but only remove the list item once the user leaves the view or the app? Many thanks!
Upvotes: 2
Views: 214
Reputation: 1002
on your highest parent view for example if your higher parent is Vstack then use
body {
VStack {
}
.onDisappear {
//perform removal of items
}
}
}
Upvotes: 1